【发布时间】:2022-10-06 00:30:14
【问题描述】:
我正在尝试制作一个简单的回显程序,其中包括一个客户端和一个在套接字编程下使用 TCP 协议的服务器。为此,我使用 C 来编写 server.c 和 client.c。
我遇到的问题是在 server.c 程序中,我试图在建立连接后从客户端接收消息,但一直失败。这发生在服务器程序中的 recv() API 中。这是两个程序:
1.)服务器.c:
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/ip.h>
int main(){
//Let\'s create a socket here.
int serverSoc = socket(PF_INET, SOCK_STREAM,0);
struct sockaddr_in serverInfo;
serverInfo.sin_family = PF_INET;
serverInfo.sin_port = htons(8000);
serverInfo.sin_addr.s_addr = INADDR_ANY;
socklen_t addrLen = (socklen_t)sizeof(serverInfo);
//We need to bind the socket to the port number. So, here we make use of the in-built struct
if(bind(serverSoc,(struct sockaddr*)&serverInfo,addrLen)<0){
printf(\"The binding of the server socket with the given port has been unsuccessful\\n.\");
return -1;
}
//After binding the socket, here we listen for connections from the clients
if(listen(serverSoc, 1)<0){ //We used 1 here in the int backlog because that specifies the number of pending connections that the queue will hold. This will help in the case when multiple clients try to connect to the server.
printf(\"We have an error in connecting to the client(s)\\n\");
return -1;
}
if(accept(serverSoc,(struct sockaddr*)&serverInfo,&addrLen)<0){
printf(\"We have an error accepting the connections from the client(s).\\n\");
return -1;
}
char recvBuff[50];
if(recv(serverSoc,&recvBuff,sizeof(recvBuff),0)<0){
printf(\"We have an error receiveing messages from the client.\\n\");
printf(\"%zd\\n\",recv(serverSoc,recvBuff,sizeof(recvBuff),0));
return -1;
}else{
printf(\"The size of recvBuff is:%d\\n\",(int)sizeof(recvBuff));
printf(\"%s\\n\",recvBuff);
}
char sendBuff[50] = \"hello\";
if(send(serverSoc,&sendBuff,sizeof(sendBuff),0)<0){
printf(\"We have an error sending message to the client.\\n\");
return -1;
}else{
printf(\"%zd\\n\",send(serverSoc,&sendBuff,sizeof(sendBuff),0));
}
return 0;
}
2.) 客户端.c:
#include<stdio.h>
#include<netinet/in.h>
#include<netinet/ip.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<string.h>
#include<unistd.h>
int main(){
char clientRecv[50];
char clientSend[50] = \"Hello Server\\n\";
//Here, we create a socket for the client with the in-built function socket that we get from sys/socket.h, socket(int domain, int type , int protocol)
int clientSoc = socket(PF_INET,SOCK_STREAM,0);
printf(\"The client socket has been assigned the value of: %d\\n\",clientSoc);
if(clientSoc < 0){
printf(\"We have an error\");
return (-1);
}
//all the struct(s) that are needed for the program
struct sockaddr_in serverInfo;
serverInfo.sin_family = PF_INET;
serverInfo.sin_port = htons(8000);
serverInfo.sin_addr.s_addr = INADDR_ANY;
//Connecting the client to the server
if((connect(clientSoc,(struct sockaddr*)&serverInfo,(socklen_t)sizeof(serverInfo)))< 0){
printf(\"Not connecting\");
return(-1);
}
if(send(clientSoc,&clientSend,sizeof(clientSend),0)<0){
printf(\"We are having an error in sending the message.\\n\");
return -1;
}
int clientRecvmsg = (int)recv(clientSoc, &clientRecv,sizeof(clientRecv),0);
if(clientRecvmsg<0){
printf(\"We have error receiving messages from the server.\\n\");
}else{
printf(\"clientRecvmsg: %s\\n\", clientRecv);
return -1;
}
return 0;
}
-
accept返回用于与客户端通信的新套接字,如果accept不成功,则返回 -1。 -
你真的应该使用
getaddrinfo而不是那些过时的 IPv4 东西。 -
@KompjoeFriek,我应该将接受函数中的第二个和第三个参数设置为 NULL 吗?
-
@Cheatah,感谢您的回复,我将研究“getaddrinfo”是什么并尝试实现它。
标签: c sockets unix server client