recv() 接收到的是0,这是为什么呢?

yirol_ 发布于 2016/05/26 12:24
阅读 655
收藏 0

在一个C/S模式的系统中,client发送操作类别后,能够正确收到服务器的响应。但是再次利用该连接发送其他信息时,服务器 recv()收到的内容竟然是0?

求大神解救那?

客户端程序。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#define server_addr "192.168.0.131"
#define server_port "5000"

int main(int argc,char *argv[])
{
	char *servIP = server_addr;
	
	in_port_t servPort = atoi(server_port);
	int sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	if(sock < 0)
	{
		puts("socket() failed!\n");
		exit(1);
	}	

	struct sockaddr_in servAddr;
	memset(&servAddr,0,sizeof(servAddr));

	servAddr.sin_family = AF_INET;

	int rtnVal = inet_pton(AF_INET,servIP,&servAddr.sin_addr.s_addr);

	if(rtnVal <= 0)
	{
		puts("inet_pton failed");
		exit(1);
	}

	servAddr.sin_port = htons(servPort);

	if(connect(sock,(struct sockaddr *)&servAddr,sizeof(servAddr)) < 0)
	{
		puts("connect() failed\n");
		exit(1);
	}
	
//	size_t echoStringLen = strlen(echoString);
	
	ssize_t numBytes = send(sock,"user_add_question",sizeof("user_add_question"),0);

	if(numBytes < 0)
	{
		puts("send() failed!\n");
		exit(1);
	}


	unsigned int totalBytesRcvd = 0;
	fputs("Received:",stdout);

	char buffer[BUFSIZ] = {0};
	while(totalBytesRcvd > 0)
	{

		numBytes = recv(sock,buffer,BUFSIZ -1 ,0);
		if(numBytes < 0)
		{
			puts("Recv() failed!\n");
			exit(1);
		}
		else if(numBytes == 0)
		{
			puts("connection closed prematurely");
			exit(1);
		}

		totalBytesRcvd += numBytes;
		buffer[numBytes] = '\0';
		if(!strcmp(buffer,"recv_true"));
		{
			char tmp[] = "kakarot|西安市哪里好?|如题。" ;
			fputs("发送用户问题:",stdout);
			ssize_t num = send(sock,tmp,strlen(tmp),0);
			if(num < 0)
			{
				puts("问题发送失败!\n");
			}
		}

	}
	int num = recv(sock,buffer,BUFSIZ -1,0);
	buffer[num] = '\0';
	puts(buffer);
	puts("\n");
	//puts(echoString);
	close(sock);
	exit(0);
}
服务端程序
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<malloc.h>
#include<mysql/mysql.h>

#define server_port "5000"
#define MAXPENDING 1000


void dealSystemError(const char *msg);
void dealUserError(const char *msg);
void getClientInformation(int clntSocket);


void recv_question(int clntSocket);
void recv_question(int clntSocket)
{
	MYSQL *conn;
	if(open_mysql(&conn))
	{
		char buffer[BUFSIZ];
		memset(buffer,sizeof(char) * BUFSIZ,0);
		ssize_t numBytesRcvd = recv(clntSocket,buffer,BUFSIZ,0);

		if(numBytesRcvd < 0)
			puts("recv() failed!\n");
		char *result_gather[4];
		int i=0;
		char *tmp ;
		tmp = buffer;            
                printf("\t%d\n",numBytesRcvd); //这里的结果是0
		puts(tmp);
		puts(buffer);
		result_gather[i] = strsep(&tmp,"|");
		while(result_gather[i])
		{
			puts(result_gather[i]);
			i++;
			result_gather[i] = strsep(&tmp,"|");
		}
		char sql[BUFSIZ] = {0};
		strcpy(sql,"insert into user_question(account,title,content)values('");
		strcat(sql,result_gather[0]);
		strcat(sql,"','");
		strcat(sql,result_gather[1]);
		puts(sql);
		strcat(sql,"','");
		strcat(sql,result_gather[2]);
		strcat(sql,"')");
		puts(sql);
		int res = mysql_query(conn,sql);
		if(res)
		{
			printf("insert error:%s\n",mysql_error(conn));
		}
		else
		{
			int ok = mysql_affected_rows(conn);
			mysql_commit(conn);
			mysql_close(conn);
			if(ok >= 1)
			{
				send(clntSocket,"recv_question_true",sizeof("recv_question_true"),0);
			}
			else
			{
				send(clntSocket,"recv_question_false",sizeof("recv_question_false"),0);
			}
		}

	}
	else
	{
		puts("open mysql faile!");
		
	}
}
int open_mysql(MYSQL **conn);
int open_mysql(MYSQL **conn)
{
	*conn = mysql_init(NULL);

	if(!(*conn))
		return -1;
	*conn = mysql_real_connect(*conn,"localhost","root","123456","user_information_system",0,NULL,0);

	if(mysql_set_character_set(*conn,"utf8"))
	{
		fprintf(stderr,"error %s\n",mysql_error(*conn));
	}
	if(*conn)
	{
		return 1;
	}
	else
		return -1;
}
void getClientInformation(int clntSocket)
{
	char buffer[BUFSIZ];
	memset(buffer,sizeof(buffer)*BUFSIZ,0);
	printf("%d\n",BUFSIZ);

	ssize_t numBytesRcvd = recv(clntSocket,buffer,BUFSIZ,0);
	printf("%d\n",BUFSIZ);

	if(numBytesRcvd < 0)
	{
		dealSystemError("recv() failed!\n");
		send(clntSocket,"recv_false",strlen("recv_false"),0);
	}

	puts(buffer);
	send(clntSocket,"recv_true",strlen("recv_true"),0);
	//char *tmp = NULL;
	//sqlLen = strlen(buffer) + 1;
	//tmp = (char *)calloc(sqlLen,sizeof(char));

	//strcpy(tmp,buffer);

	//*recvInfo = tmp;
	recv_question(clntSocket);

}
void dealUserError(const char * msg)
{
	FILE * fd;
	fd = fopen("dealError.txt","a+");

	fputs(msg,fd);
	fclose(fd);
}
void dealSystemError(const char *msg)
{
	FILE * fd;
	fd = fopen("dealError.txt","a+");

	fputs(msg,fd);

	fclose(fd);
}
void main(void)
{
	in_port_t servPort = atoi(server_port);

	int servSock;

	if((servSock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)) < 0)	
	{
		dealSystemError("socket() failed!\n");		
	}

	struct sockaddr_in servAddr;
	memset(&servAddr,0,sizeof(servAddr));
	servAddr.sin_family = AF_INET;
	servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servAddr.sin_port = htons(servPort);

	if(bind(servSock,(struct sockaddr *)&servAddr,sizeof(servAddr)) < 0)
	{
		dealSystemError("bind() failed!\n");
	}
	if(listen(servSock,MAXPENDING) < 0)
	{
		dealSystemError("listen() failed!\n");
	}
	
	while(1)
	{
		struct sockaddr_in clientAddr;
		socklen_t clientAddrLen = sizeof(clientAddr);

		int clientSock = accept(servSock,(struct sockaddr *)&clientAddr,&clientAddrLen);
		if(clientSock < 0)
		{
			dealSystemError("accept() failed!\n");
		}
		char clientName[INET_ADDRSTRLEN];

		if(inet_ntop(AF_INET,&clientAddr.sin_addr.s_addr,clientName,sizeof(clientName)) == NULL)
		{
			dealSystemError("Unable to get client address!\n");
		}

		getClientInformation(clientSock);
	}
}



加载中
0
乌龟壳
乌龟壳
《UNIX环境高级编程》相关章节通读一遍,不然很多细节不知道,写出来的程序坑连坑。
yirol_
yirol_
我找到错误了,是一个很傻的错误,,谢谢了
0
jQer
jQer
说明对端已经 close()
yirol_
yirol_
客户端没有close() 是我根本就没有发过去,那个while()循环我根本就没有进去。。我好煞笔
返回顶部
顶部