gpt4 book ai didi

c套接字编程,接收线路时出错

转载 作者:行者123 更新时间:2023-11-30 14:23:15 26 4
gpt4 key购买 nike

我使用 C 中的 Socket 编程修改了一个简单的聊天程序的代码。我的问题是我必须在服务器或客户端输入两行才能接收对方的一行。我上传了一张图片来解释更多信息。任何帮助表示赞赏

SERVER
#include <stdio.h>
#include <sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<strings.h>
#include<unistd.h>
#define SERVER_TCP_PORT 3000
#define BUFLEN 256

int main (int argc,char **argv)
{
int n,bytes_to_read;
int sd,new_sd,client_len,port;
struct sockaddr_in server,client;
char *bp,buf[BUFLEN];

switch (argc) {
case 1:
port = SERVER_TCP_PORT;
break;
case 2:
port = atoi (argv[1]);
break;
default:
fprintf (stderr,"Usage: %s[port]\n",argv[0]);
exit(1);
}
/*Create a system socket */

if ((sd = socket(AF_INET,SOCK_STREAM,0))==-1){
fprintf(stderr,"Can't create a socket\n");
exit(1);
}

/*Bind an address to the socket*/

bzero ((char*)&server,sizeof(struct sockaddr_in));
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(sd,(struct sockaddr *)&server,sizeof(server))==-1){
fprintf(stderr,"can't bind name to socket\n");
exit(1);
}
/*queue up to 5 connect requests*/

listen(sd,5);
while(1){
client_len = sizeof(client);
if((new_sd = accept(sd,(struct sockaddr *)&client,&client_len))==-1){
fprintf(stderr,"can't accept client \n");
exit(1);
}

if(fork())
{
printf("\n Server listening to clients on port 2000");
printf("\n Server got request from%s",inet_ntoa(client.sin_addr));
printf("\n SEND DATA<q or Q to quit> :%s",buf);
scanf("%s",buf);
while((strcmp(buf,"q")!=0)&&(strcmp(buf,"Q")!=0))
{
send(new_sd,buf,30,0);
scanf("%s",buf);

}
send(new_sd,"q",5,0);
}
else{
n = recv(new_sd,buf,30,0);
while((strcmp(buf,"q")!=0)&&(strcmp(buf,"Q")!=0))
{
printf("\nRECEIVED DATA =%s",buf);
n = recv(new_sd,buf,30,0);

}
printf("\n Bye");
send(new_sd,"EOF",4,0);
close(sd);
close(new_sd);
}
}
return 0;
}



Client
#include <stdio.h>
#include <sys/types.h>
#include<sys/socket.h>
#include <netdb.h>
#include<stdlib.h>
#include<strings.h>
#include <netinet/in.h>

#define SERVER_TCP_PORT 3000
#define BUFLEN 256
int main(int argc,char **argv)
{

int n,bytes_to_read;
int sd,port;
struct hostent *hp;
struct sockaddr_in server;
char *host,*bp,rbuf[BUFLEN],sbuf[BUFLEN];
switch(argc) {
case 2:
host = argv[1];
port = SERVER_TCP_PORT;
break;
case 3:
host = argv[1];
port = atoi(argv[2]);
break;
default:
fprintf(stderr,"Usage: %s host[port] \n",argv[0]);
exit(1);
}

/*Create a stream socket*/

if ((sd=socket(AF_INET,SOCK_STREAM,0))==-1){
fprintf(stderr,"Can't create a socket\n");
exit(1);
}
bzero ((char *)&server,sizeof(struct sockaddr_in));
server.sin_family = AF_INET;
server.sin_port = htons(port);
if((hp = gethostbyname(host))==NULL){
fprintf (stderr,"Can't get server's address\n");
exit(1);
}
bcopy(hp->h_addr,(char *)&server.sin_addr,hp->h_length);
/*connecting to the server*/

if (connect (sd,(struct sockaddr *)&server,sizeof(server))==-1){
fprintf(stderr,"Can't connect \n");
exit(1);
}
printf("Connected:server's address is %s\n",hp->h_name);

if(fork())
{
bzero(rbuf,30);
printf("\n SEND DATA<q or Q to quit> :%s",rbuf);
scanf("%s",rbuf);
while((strcmp(rbuf,"q")!=0)&&(strcmp(rbuf,"Q")!=0))
{
send(sd,rbuf,30,0);
scanf("%s",rbuf);

}
send(sd,"q",5,0);
}

else{
n = recv(sd,rbuf,30,0);
while((strcmp(rbuf,"q")!=0)&&(strcmp(rbuf,"Q")!=0))
{
printf("\nRECEIVED DATA =%s",rbuf);
n = recv(sd,rbuf,30,0);

}
printf("\n Bye");
send(sd,"EOF",4,0);
close(sd);
}

return 0;
}

最佳答案

您每次仅发送/接收 30 个字节。也许那应该是BUFLEN?更好的是,在消息的前面有一个固定大小的 block ,其中包含消息的 strlen

例如:

int size = htonl(strlen(buf)+1);
send(new_sd, size, sizeof(size), 0);
send(new_sd, buf, size, 0);

客户:

int size;
recv(sd, size, sizeof(size),0);
size = ntohl(size);
recv(sd, buf, size, 0);

为清楚起见,已省略错误检查。

关于c套接字编程,接收线路时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13008175/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com