gpt4 book ai didi

c - 写入套接字

转载 作者:行者123 更新时间:2023-11-30 21:03:15 24 4
gpt4 key购买 nike

我正在使用套接字制作一个简单的一对一服务器客户端聊天应用程序。基本上有一个服务器可以一次与一个客户端通信。我想做的是客户端应该向服务器发送一个字符串,然后服务器必须将其发送回客户端并更改字符串的大小写(从大写到小写,反之亦然)。问题是字符串已发送到服务器,但服务器从未响应,因为客户端无法发送其他字符串。程序的输出:-

root@User:~/Desktop/Aadil/SystemPracticum/Programs/Assignment5# ./Server 4000

来自客户端的消息是message1

root@User:~/Desktop/Aadil/SystemPracticum/Programs/Assignment5# ./Client localhost 4000

输入消息message1

输入消息message2

谢谢你这是我的代码服务器.c

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
void ChangeCase(char *string){
int i = 0;
while(string[i]){
// printf("converting\n");
if(string[i] <= 90 && string[i] >= 65)
string[i] += 32;
else
string[i] -= 32;
++i;
}

}
int main(int counter, char *string[]){
if(counter < 2){
perror("erro! please provide port no.\n");
}else{
int server_socket_file_descriptor,client_socket_file_descriptor,
port_no,message_length,client_length;
char buffer[256];//buffer to be used for storing messages
struct sockaddr_in server_address,client_address;

server_socket_file_descriptor = socket(AF_INET,SOCK_STREAM,0);
/*it creates new socket the first argument AF_INET is used for internet domain
and second argument SOCK_STREAM is used for stream socket
third argument 0 means the default protocol for stram socket which is tcp*/
if(server_socket_file_descriptor < 0)
perror("\t\t\t\t=====!!!cant create a socket!!!=====\n");

bzero((char*)&server_address,sizeof(server_address));//set all value to 0

//set port no. by converting port from char* to integer
port_no = atoi(string[1]);

/*now initialize the server_address
server_address is a struct of sockaddr_in type which has four field in it
we need to initialize 3 of them
*/
server_address.sin_family = AF_INET;
//convert port no. to network byte order
server_address.sin_port = htons(port_no);
//set server ip address to the machines ip address in my case it is 10.8.3.236
server_address.sin_addr.s_addr=INADDR_ANY;

/*now we need to bind the server with socket created*/
if(bind(server_socket_file_descriptor,(struct sockaddr*)&server_address,
sizeof(server_address)) < 0){
perror("\t\t\t\t\t====error in binding====\n");
return 0;
}
//since socket is bind correctly I am not checking for the error
listen(server_socket_file_descriptor,8);
/*listening to socket. 8 represent the maximum client that
can wait in queue to connect to the server*/

//we are done with the server :D

client_length = sizeof(client_address);
client_socket_file_descriptor = accept(server_socket_file_descriptor,
(struct sockaddr*)&client_address,
&client_length);
if(client_socket_file_descriptor < 0)
perror("\t\t\t\t unable to connect to client");

while(1){
bzero(buffer,256);
message_length = read(client_socket_file_descriptor,buffer,255);

if(message_length < 0)
perror("\t\t\t\t error in reading from socket\n");

printf("\t\t\t\tthe message from client is %s\n",buffer);
ChangeCase(buffer);

message_length = write(client_socket_file_descriptor,buffer,sizeof(buffer));
if(message_length < 0)
perror("\t\t\t\t error writing to socket\n");
}
return 0;
}
}

客户端.c

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>

int main(int argc, char *argv[])
{
int file_descriptor,message_length,port_no;
char Buffer[256];//to store the message
//to store the address of the server to which we want to connect
struct sockaddr_in server_address;
struct hostent *server;//hostent defines the host computer on internet
if(argc < 3){
printf("\t\t\t please provide ip address and port no.\n");
return 1;
}
port_no = atoi(argv[2]);

if((file_descriptor = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Error : Could not create socket \n");
return 1;
}

memset(&server_address, '0', sizeof(server_address));

server_address.sin_family = AF_INET;
//convert port no. to network byte order
server_address.sin_port = htons(port_no);
//set server ip address to the machines ip address in my case it is 10.8.3.236
server_address.sin_addr.s_addr = INADDR_ANY;

if(connect(file_descriptor,(struct sockaddr*)&server_address,
sizeof(server_address))<0){
perror("error in connection\n");
return 1;
}

while(1){
memset(Buffer, '0',sizeof(Buffer));
printf("\t\t\t\t\tenter the message\n");
fgets(Buffer,255,stdin);
message_length = write(file_descriptor,Buffer,strlen(Buffer));

if(message_length<0)
perror("\t\t\t\terror in writing\n");

memset(Buffer,'0',sizeof(Buffer));
message_length = read(file_descriptor,Buffer,255);

if(message_length < 0)
perror("\t\t\terror in reading from buffer\n");
else{
printf("%s\n",Buffer);
}
}

return 0;
}

最佳答案

我怀疑问题出在服务器代码中的这一行:

message_length = write(client_socket_file_descriptor,buffer,sizeof(buffer));

请注意,该行始终将 256 字节发送回客户端。对于像“message1”这样的字符串,这意味着它将发回“MESSAGE1”,后跟 248 NUL/零字节。

根据 TCP 堆栈如何决定分解这些字节,您的客户端的 read() 调用可能会以不同的部分序列接收这些字节,并且如果它接收到的任何部分序列以 NUL/零字节开头,它将打印出来作为空字符串。

为了更好地了解发生了什么,您可以在客户端中替换此行:

printf("%s\n",Buffer);

像这样:

printf("[%s]\n",Buffer);

我还建议更改您的服务器以指定 strlen(buffer) 作为 write() 的最终参数,而不是 sizeof(buffer)。

关于c - 写入套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26156682/

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