gpt4 book ai didi

c - 使用套接字和 TCP 的 C 语言服务器客户端程序

转载 作者:行者123 更新时间:2023-11-30 21:28:53 34 4
gpt4 key购买 nike

我使用套接字编程编写了一个在服务器和客户端之间进行简单文本聊天的程序。服务器端运行正常,因为它正在监听,但是当我运行客户端程序时,它运行但没有显示输出,这意味着如果我在客户端输入,服务器端不会显示任何内容。为什么我没有收到客户端的任何回复?

    /****************** CLIENT CODE ****************/

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

int main(){
int clientSocket,n;
char buffer[256];
struct sockaddr_in serverAddr;
socklen_t addr_size;

/*---- Create the socket. The three arguments are: ----*/
/* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */
clientSocket = socket(PF_INET, SOCK_STREAM, 0);

/*---- Configure settings of the server address struct ----*/
/* Address family = Internet */
serverAddr.sin_family = AF_INET;
/* Set port number, using htons function to use proper byte order */
serverAddr.sin_port = htons(7891);
/* Set IP address to localhost */
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
/* Set all bits of the padding field to 0 */
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);

/*---- Connect the socket to the server using the address struct ----*/
addr_size = sizeof serverAddr;
connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);

if (connect(clientSocket,(struct sockaddr *) &serverAddr,sizeof(serverAddr)) < 0)
error("ERROR connecting");
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(clientSocket,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(clientSocket,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer);
close(clientSocket);
/*---- Read the message from the server into the buffer ----*/
//recv(clientSocket, buffer, 1024, 0);

/*---- Print the received message ----*/
//printf("Data received: %s",buffer);

return 0;
}


/****************** SERVER CODE ****************/

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

int main(){
int welcomeSocket, newSocket;
char buffer[256];
int n;
struct sockaddr_in serverAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size;

/*---- Create the socket. The three arguments are: ----*/
/* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */
welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);

/*---- Configure settings of the server address struct ----*/
/* Address family = Internet */
serverAddr.sin_family = AF_INET;
/* Set port number, using htons function to use proper byte order */
serverAddr.sin_port = htons(7891);
/* Set IP address to localhost */
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
/* Set all bits of the padding field to 0 */
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);

/*---- Bind the address struct to the socket ----*/
bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));

/*---- Listen on the socket, with 5 max connection requests queued ----*/
if(listen(welcomeSocket,5)==0)
printf("Listening\n");
else
printf("Error\n");

/*---- Accept call creates a new socket for the incoming connection ----*/
addr_size = sizeof serverStorage;
newSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);

/*---- Send message to the socket of the incoming connection ----*/
bzero(buffer,256);
n = read(welcomeSocket,buffer,255);
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %s\n",buffer);
n = write(welcomeSocket,"I got your message",18);
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %s\n",buffer);
n = write(welcomeSocket,"I got your message",18);
if (n < 0) error("ERROR writing to socket");
close(newSocket);
close(welcomeSocket);

return 0;
}

最佳答案

在服务器代码中,read() 应使用从 accept() 返回的套接字描述符,而不是从调用 socket() 返回的套接字描述符>。因此,请尝试使用 newSocket 而不是 welcomeSocket 进行阅读。

n = read(newSocket,buffer,255);

关于c - 使用套接字和 TCP 的 C 语言服务器客户端程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37033443/

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