新手请教一个socket问题

Schengshuo 2017-11-01 05:12:57
问题:客户端发送一次数据后,第二次服务端接收不了啦,只能接收一次是为什么





服务端代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>


int main()
{
int sfd, cfd;
struct sockaddr_in server_addr;
char buff[10];

//创建套接字
if( (sfd=socket(AF_INET, SOCK_STREAM, 0))<0 ){
perror("create socket error!");
exit(0);
}

memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8888);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY); //通配符地址

//防止服务器关闭后需要等待TIME_WAIT状态才能才继续使用address
int on=1;
if ( setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))<0 ){
perror("set socket opt fail!\n");
exit(0);
}
//绑定套接字
if( bind(sfd, (struct sockaddr *)&server_addr, sizeof(server_addr))==-1 ){
perror("bind has error!\n");
exit(0);
}

//监听套接字
if( listen(sfd, 10)==-1 ) {
perror("listen has error!\n");
exit(0);
}

//接收客户端
while(1){
puts("waitting for client on 888");
if( (cfd = accept(sfd, (struct sockaddr *)NULL, NULL)) > -1 ) {
printf("new socket fd:%d\n", cfd);
}

int n;
if( (n = recv(cfd, buff, strlen(buff), 0)) < 0 ) {
puts("recv data fail\n");
exit(0);
}
buff[n] = '\0';
printf("recv msg from client: %s\n", buff);
puts(buff);
close(cfd);
}

//关闭close();
close(sfd);

return 0;
}


客户端代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>

int main()
{
int cfd;
struct sockaddr_in server_addr;
char buff[10];

//创建socket
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8888);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

cfd = socket(AF_INET, SOCK_STREAM, 0);
if( cfd<0 ) {
perror("create socket error\n!");
exit(0);
}

//连接
if( connect(cfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) ==-1 ){
perror("connect has error!\n");
exit(1);
}
printf("connect with destination host...\n");

while(1){
memset(buff, 0, strlen(buff));
scanf("%s", buff);
send(cfd, buff, strlen(buff), 0);
}


//关闭close();
close(cfd);
}
...全文
221 1 1 打赏 收藏 举报
写回复
1 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Schengshuo 2017-11-01
问题已经解决了: 原来recv还需要循环接收
引用 楼主 Schengshuo 的回复:
问题:客户端发送一次数据后,第二次服务端接收不了啦,只能接收一次是为什么 服务端代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>


int main()
{
	int sfd, cfd;
	struct sockaddr_in server_addr;
	char buff[10];

	//创建套接字
	if( (sfd=socket(AF_INET, SOCK_STREAM, 0))<0 ){
		perror("create socket error!");
		exit(0);
	}
	
	memset(&server_addr, 0, sizeof(server_addr));
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(8888);
	server_addr.sin_addr.s_addr = htonl(INADDR_ANY);		//通配符地址

	//防止服务器关闭后需要等待TIME_WAIT状态才能才继续使用address
	int on=1;
	if ( setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))<0 ){
		perror("set socket opt fail!\n");
		exit(0);
	}
	//绑定套接字
	if( bind(sfd, (struct sockaddr *)&server_addr, sizeof(server_addr))==-1 ){
		perror("bind has error!\n");
		exit(0);
	}
	
	//监听套接字
	if( listen(sfd, 10)==-1 ) {
		perror("listen has error!\n");
		exit(0);
	}
	
	//接收客户端
	while(1){
		puts("waitting for client on 888");
		if( (cfd = accept(sfd,  (struct sockaddr *)NULL, NULL)) > -1 ) {
			printf("new socket fd:%d\n", cfd);
		}
		
		int n;
		if( (n = recv(cfd, buff, strlen(buff), 0)) < 0 ) {
			puts("recv data fail\n");
			exit(0);
		}
	    buff[n] = '\0';
    	printf("recv msg from client: %s\n", buff);
		puts(buff);
		close(cfd);
	}
	
	//关闭close();
	close(sfd);
	
	return 0;
}
客户端代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>

int main()
{
	int cfd;
	struct sockaddr_in server_addr;
	char buff[10];
	
	//创建socket
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(8888);
	server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
	
	cfd = socket(AF_INET, SOCK_STREAM, 0);
	if( cfd<0 ) {
		perror("create socket error\n!");
		exit(0);
	}
	
	//连接
	if( connect(cfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) ==-1 ){
		perror("connect has error!\n");
		exit(1);
	}
	printf("connect with destination host...\n");  
	
	while(1){
		memset(buff, 0, strlen(buff));
		scanf("%s", buff);
		send(cfd, buff, strlen(buff), 0);
	}
	
	
	//关闭close();
	close(cfd);
}
  • 打赏
  • 举报
回复
相关推荐
发帖
C语言
加入

6.5w+

社区成员

C语言相关问题讨论
申请成为版主
帖子事件
创建了帖子
2017-11-01 05:12
社区公告
暂无公告