当前位置: 技术问答>linux和unix
【大侠看我】linux tcp socket 消息传输问题
来源: 互联网 发布时间:2017-01-03
本文导语: 小弟最近学习linux下的网络编程,编译调试都没问题,就是在进行通信的时候有点问题 比如在客户端输入:123 456,到了服务器端,却会分成两段来显示... 客户端:123 客户端:456 只要是我的输入中有空格,那么在...
小弟最近学习linux下的网络编程,编译调试都没问题,就是在进行通信的时候有点问题
比如在客户端输入:123 456,到了服务器端,却会分成两段来显示...
客户端:123
客户端:456
只要是我的输入中有空格,那么在另一段的显示中都会被分割开
怎么才能让他们一起完整显示呢????求大家帮帮我哈
服务器端:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MYPORT 3490
#define BACKLOG 10
#define MAXDATASIZE 1024
int sockfd,new_fd;
pthread_t accthread,recthread;
void recmessage(void){
while(1){
int numbytes;
char buf[MAXDATASIZE];
if((numbytes = recv(new_fd,buf,MAXDATASIZE,0)) == -1){
perror("recv");
exit(1);
}
buf[numbytes] = '';
if(strcmp(buf,"exit") == 0){
printf("Client is closedn");
close(new_fd);
close(sockfd);
exit(1);
}
printf("Client:%sn",buf);
}
}
void acceptconnect(void){
struct sockaddr_in their_addr;
int sin_size;
sin_size = sizeof(struct sockaddr_in);
if((new_fd = accept(sockfd,(struct sockaddr*)&their_addr,&sin_size)) == -1){
perror("accept");
exit(1);
}
printf("server:got connection from %sn",(char*)inet_ntoa(their_addr.sin_addr));
if((pthread_create(&recthread,NULL,(void *)recmessage,NULL))!= 0){
printf("Create thread error!rn");
exit(1);
}
}
int main(void){
struct sockaddr_in my_addr; //定义一个表示套接字的结构体
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){ //创建套接字,SOCK_STREAM表示面向连接的数据流传输,也就是TCP
perror("socket");
exit(1);
}
/*以下填充套接字结构体*/
my_addr.sin_family = AF_INET; //IPv4地址族
my_addr.sin_port = htons(MYPORT); //端口
my_addr.sin_addr.s_addr = INADDR_ANY; //计算机上默认的本地地址127.0.0.1
bzero(&my_addr,sizeof(my_addr)); //为结构体分配内存
if(bind(sockfd,(struct sockaddr*)&my_addr,sizeof(struct sockaddr)) == -1){ //绑定端口
perror("bind");
exit(1);
}
if(listen(sockfd,BACKLOG) == -1){ //监听,成功返回0,失败返回1.BACKLOG是能处理的最大连接数
perror("listen");
exit(1);
}
if((pthread_create(&accthread,NULL,(void *)acceptconnect,NULL)) != 0){ //开启线程
printf("Create thread error!rn");
exit(1);
}
while(1){ //进入循环
char msg[MAXDATASIZE];
scanf("%s",msg);
if(send(new_fd,msg,strlen(msg),0) == -1){
perror("send");
close(new_fd);
exit(1);
}
if(strcmp(msg,"exit") == 0){
printf("Byebye!n");
close(new_fd);
close(sockfd);
exit(1);
}
}
return 0;
}
客户端:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 3490
#define MAXDATASIZE 1024
int sockfd;
pthread_t recthread;
void recmessage(void){
while(1){
int numbytes;
char buf[MAXDATASIZE];
if((numbytes = recv(sockfd,buf,MAXDATASIZE,0)) == -1){
perror("recv");
exit(1);
}
buf[numbytes]='';
if(strcmp(buf,"exit") == 0){
printf("Server is closedn");
close(sockfd);
exit(1);
}
printf("Server:%sn",buf);
}
}
int main(int argc,char *argv[]){
struct hostent *he;
struct sockaddr_in their_addr;
if(argc != 2){
fprintf(stderr,"usage:client hostnamen");
exit(1);
}
if((he=gethostbyname(argv[1])) == NULL){
herror("gethostbyname");
exit(1);
}
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(PORT);
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
bzero(&(their_addr.sin_zero),8);
if(connect(sockfd,(struct sockaddr *)&their_addr,sizeof(struct sockaddr)) == -1){
perror("connect");
exit(1);
}
if((pthread_create(&recthread,NULL,(void *)recmessage,NULL))!=0){
printf("Create thread error!rn");
exit(1);
}
while(1){
char msg[MAXDATASIZE];
scanf("%s",msg);
if(send(sockfd,msg,strlen(msg),0) == -1){
perror("send");
close(sockfd);
exit(1);
}
if(strcmp(msg,"exit") == 0){
printf("Byebye!n");
close(sockfd);
exit(1);
}
}
return 0;
}
各位看看吧
比如在客户端输入:123 456,到了服务器端,却会分成两段来显示...
客户端:123
客户端:456
只要是我的输入中有空格,那么在另一段的显示中都会被分割开
怎么才能让他们一起完整显示呢????求大家帮帮我哈
服务器端:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MYPORT 3490
#define BACKLOG 10
#define MAXDATASIZE 1024
int sockfd,new_fd;
pthread_t accthread,recthread;
void recmessage(void){
while(1){
int numbytes;
char buf[MAXDATASIZE];
if((numbytes = recv(new_fd,buf,MAXDATASIZE,0)) == -1){
perror("recv");
exit(1);
}
buf[numbytes] = '';
if(strcmp(buf,"exit") == 0){
printf("Client is closedn");
close(new_fd);
close(sockfd);
exit(1);
}
printf("Client:%sn",buf);
}
}
void acceptconnect(void){
struct sockaddr_in their_addr;
int sin_size;
sin_size = sizeof(struct sockaddr_in);
if((new_fd = accept(sockfd,(struct sockaddr*)&their_addr,&sin_size)) == -1){
perror("accept");
exit(1);
}
printf("server:got connection from %sn",(char*)inet_ntoa(their_addr.sin_addr));
if((pthread_create(&recthread,NULL,(void *)recmessage,NULL))!= 0){
printf("Create thread error!rn");
exit(1);
}
}
int main(void){
struct sockaddr_in my_addr; //定义一个表示套接字的结构体
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){ //创建套接字,SOCK_STREAM表示面向连接的数据流传输,也就是TCP
perror("socket");
exit(1);
}
/*以下填充套接字结构体*/
my_addr.sin_family = AF_INET; //IPv4地址族
my_addr.sin_port = htons(MYPORT); //端口
my_addr.sin_addr.s_addr = INADDR_ANY; //计算机上默认的本地地址127.0.0.1
bzero(&my_addr,sizeof(my_addr)); //为结构体分配内存
if(bind(sockfd,(struct sockaddr*)&my_addr,sizeof(struct sockaddr)) == -1){ //绑定端口
perror("bind");
exit(1);
}
if(listen(sockfd,BACKLOG) == -1){ //监听,成功返回0,失败返回1.BACKLOG是能处理的最大连接数
perror("listen");
exit(1);
}
if((pthread_create(&accthread,NULL,(void *)acceptconnect,NULL)) != 0){ //开启线程
printf("Create thread error!rn");
exit(1);
}
while(1){ //进入循环
char msg[MAXDATASIZE];
scanf("%s",msg);
if(send(new_fd,msg,strlen(msg),0) == -1){
perror("send");
close(new_fd);
exit(1);
}
if(strcmp(msg,"exit") == 0){
printf("Byebye!n");
close(new_fd);
close(sockfd);
exit(1);
}
}
return 0;
}
客户端:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 3490
#define MAXDATASIZE 1024
int sockfd;
pthread_t recthread;
void recmessage(void){
while(1){
int numbytes;
char buf[MAXDATASIZE];
if((numbytes = recv(sockfd,buf,MAXDATASIZE,0)) == -1){
perror("recv");
exit(1);
}
buf[numbytes]='';
if(strcmp(buf,"exit") == 0){
printf("Server is closedn");
close(sockfd);
exit(1);
}
printf("Server:%sn",buf);
}
}
int main(int argc,char *argv[]){
struct hostent *he;
struct sockaddr_in their_addr;
if(argc != 2){
fprintf(stderr,"usage:client hostnamen");
exit(1);
}
if((he=gethostbyname(argv[1])) == NULL){
herror("gethostbyname");
exit(1);
}
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(PORT);
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
bzero(&(their_addr.sin_zero),8);
if(connect(sockfd,(struct sockaddr *)&their_addr,sizeof(struct sockaddr)) == -1){
perror("connect");
exit(1);
}
if((pthread_create(&recthread,NULL,(void *)recmessage,NULL))!=0){
printf("Create thread error!rn");
exit(1);
}
while(1){
char msg[MAXDATASIZE];
scanf("%s",msg);
if(send(sockfd,msg,strlen(msg),0) == -1){
perror("send");
close(sockfd);
exit(1);
}
if(strcmp(msg,"exit") == 0){
printf("Byebye!n");
close(sockfd);
exit(1);
}
}
return 0;
}
各位看看吧
|
代码真够乱的,问题出在scanf()这个函数了 如果要在终端手动输入的话。有好多种方法的简单的这样写
while (fgets(buffer,sizeof(buffer),stdin)!=NULL) stdin=>就是屏幕了,这个输入完时,里面BUFFER会多个n
while (fgets(buffer,sizeof(buffer),stdin)!=NULL) stdin=>就是屏幕了,这个输入完时,里面BUFFER会多个n