当前位置: 技术问答>linux和unix
哪位能修改 关于socket 的部分简单代码
来源: 互联网 发布时间:2016-10-21
本文导语: 本帖最后由 lijianbin9 于 2010-11-13 03:47:09 编辑 helloworld_server.c #include #include #include #include #include #include #include void error_handling(char *message); int main(int argc,char *argv[]) { int serv_sock; int clnt_sock; struct sockaddr_in serv_addr; struct ...
helloworld_server.c
#include
#include
#include
#include
#include
#include
#include
void error_handling(char *message);
int main(int argc,char *argv[])
{
int serv_sock;
int clnt_sock;
struct sockaddr_in serv_addr;
struct sockaddr_in clnt_addr;
int clnt_addr_size;
char message[] = "hello world!n";
if(argc != 2){
printf("Usage : %sn",argv[0]);
}
serv_sock = socket(AF_INET, SOCK_STREAM, 0);
if(serv_sock == -1)
error_handling("socket() error!!");
memset(&serv_addr,0,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(atoi(argv[1]));
if(bind(serv_sock,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) == -1)
error_handling("bind() error");
if(listen(serv_sock,5) == -1)
error_handling("listen() error");
clnt_addr_size = sizeof(clnt_addr);
clnt_sock = accept(serv_sock,(struct sockaddr*)&clnt_addr,&clnt_addr_size);
//问题1:这里怎么修改 accept 可以让 client多次访问,并不是 访问一次之后 server结束!!
if(clnt_sock == -1)
error_handling("accept() error");
write(clnt_sock,message,sizeof(message));
close(clnt_sock);
return 0;
}
void error_handling(char *message)
{
fputs(message,stderr);
fputc('n',stderr);
exit(1);
}
//=================================================================
//helloworld_client
#include
#include
#include
#include
#include
#include
#include
void error_handling(char *message);
int main(int argc,char *argv[])
{
int sock;
char message[30];
int str_len;
struct sockaddr_in serv_addr;
if(argc != 3){
printf("Usage : %sn",argv[0]);
exit(1);
}
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock == -1)
error_handling("socket() error!!");
memset(&serv_addr,0,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
serv_addr.sin_port = htons(atoi(argv[2]));
if(connect(sock,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) == -1)
error_handling("connect() error");
str_len = read(sock,message,sizeof(message) - 1);
if(str_len == -1)
error_handling("read() error");
message[str_len] = 0;
printf("Messge from server:%sn",message);
close(sock);
return 0;
}
void error_handling(char *message)
{
fputs(message,stderr);
fputc('n',stderr);
exit(1);
}
//==================================================================
问题2:在以上的代码的 基础上,怎么 通过 read(),write(), fputs() / fgets() / strcmp()
实现 在client输入信息返回server,在返回 client。
|
//helloworld_server.c
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXBUFSIZE 4096
void error_handling(char *message);
int main(int argc,char *argv[])
{
int serv_sock;
int clnt_sock;
struct sockaddr_in serv_addr;
struct sockaddr_in clnt_addr;
int clnt_addr_size;
char alRcvbuf[MAXBUFSIZE + 1];
char alSndbuf[MAXBUFSIZE + 1];
char alIp[256 + 1];
int ilMsglen;
if(argc != 2)
{
printf("Usage : %s n",argv[0]);
exit(1);
}
serv_sock = socket(AF_INET, SOCK_STREAM, 0);
if(serv_sock == -1)
error_handling("socket() error!!");
memset(&serv_addr,0,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(atoi(argv[1]));
if(bind(serv_sock,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) == -1)
error_handling("bind() error");
if(listen(serv_sock,5) == -1)
error_handling("listen() error");
while (1)
{
ilMsglen = 0;
clnt_addr_size = sizeof(clnt_addr);
memset(alIp,0x00,sizeof(alIp));
memset(alRcvbuf,0x00,sizeof(alRcvbuf));
memset(alSndbuf,0x00,sizeof(alSndbuf));
clnt_sock = accept(serv_sock,(struct sockaddr*)&clnt_addr,&clnt_addr_siz
e);
if(clnt_sock == -1)
{
error_handling("accept() error");
close(clnt_sock);
continue;
}
while (1)
{
ilMsglen = read(clnt_sock,alRcvbuf,MAXBUFSIZE);
if (alRcvbuf[0] == 'q' || strncmp(alRcvbuf,"quit",4) == 0)
{
close(clnt_sock);
break;
}
printf("Rcving request from [%s][%d]:n%s",
inet_ntop(AF_INET,&clnt_addr.sin_addr,alIp,sizeof(alIp)),
ntohs(clnt_addr.sin_port),
alRcvbuf);
memcpy(alSndbuf,alRcvbuf,ilMsglen);
write(clnt_sock,alSndbuf,ilMsglen);
}
close(clnt_sock);
}
return 0;
}
void error_handling(char *message)
{
fputs(message,stderr);
fputc('n',stderr);
}
//helloworld_client.c
#include
#include
#include
#include
#include
#include
#include
#define MAXBUFSIZE 1024
void error_handling(char *message);
int main(int argc,char *argv[])
{
int sock;
int str_len;
char alSndbuf[MAXBUFSIZE + 1];
char alRcvbuf[MAXBUFSIZE + 1];
struct sockaddr_in serv_addr;
if(argc != 3)
{
printf("Usage : %s n",argv[0]);
exit(1);
}
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock == -1)
error_handling("socket() error!!");
memset(&serv_addr,0,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
serv_addr.sin_port = htons(atoi(argv[2]));
if(connect(sock,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) == -1)
error_handling("connect() error");
while(1)
{
memset(alSndbuf,0x00,sizeof(alSndbuf));
memset(alRcvbuf,0x00,sizeof(alRcvbuf));
fgets(alSndbuf,MAXBUFSIZE,stdin);
write(sock,alSndbuf,strlen(alSndbuf));
str_len = read(sock,alRcvbuf,MAXBUFSIZE);
if(str_len == -1)
error_handling("read() error");
else if (str_len == 0)
{
break;
}
printf("Messge from server:%sn",alRcvbuf);
}
close(sock);
return 0;
}
void error_handling(char *message)
{
fputs(message,stderr);
fputc('n',stderr);
exit(1);
}
$./helloworld_server 5000
Rcving request from [192.168.5.3][35017]:
hello
Rcving request from [192.168.5.3][35017]:
world
./helloworld_client 192.168.5.3 5000
hello
Messge from server:hello
world
Messge from server:world
quit