gpt4 book ai didi

c - unix tcp 客户端/服务器奇怪的行为

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

我一直在尝试学习unix网络编程,因此我尝试编写一个客户端/服务器程序,其中客户端发送消息,服务器返回转换为大写字母的消息。

当我运行服务器并将客户端连接到我自己的计算机并发送一些测试字符串时,它工作正常,直到我之前输入的内容写入屏幕。

我怀疑这与缓冲区有关。这是启动服务器后运行的示例:

can@ubuntu:~$ cd 桌面

can@ubuntu:~/Desktop$ ./tcpuppcli 127.0.0.1

输入要回显的字符串:test

回声响应:测试

输入要回显的字符串:string2

回声响应:STRING2

输入要回显的字符串:string3

回声响应:STRING3

输入要回显的字符串:aaaaaaaaafsfagd

回声响应:AAAAAAAAAFSFAGD

输入要回显的字符串:gdsgsg

回声响应:GDSGSG

AAFSFAGD ----------->!这是奇怪的行,其中包含来自先前输入的字符!

输入要回显的字符串:^C

can@ubuntu:~/Desktop$

服务端和客户端的代码如下:

// methods with prefix w_ are just error checking wrappers from UNP by Stevens    
// server
#include "socketwrap.h" // headers necessary and constants like MAXLINE
#include <ctype.h>

void sigchld_handler( int sig);
void str_upper( int connfd);
void toUpperCase( char buffer[], int length);

int main( int argc, char** argv)
{
int listenfd;
int connfd;
pid_t childpid;
struct sockaddr_in serverAddress;
struct sockaddr_in clientAddress;
socklen_t length;
struct sigaction sa;

// Create the socket
listenfd = w_socket( AF_INET, SOCK_STREAM, 0);

// Clear the serverAddress structure
bzero( &serverAddress, sizeof( serverAddress));
// Set up the serverAddress structure
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = htonl( INADDR_ANY);
serverAddress.sin_port = htons( 11979);

// Bind the socket to a well-defined port
w_bind( listenfd, (struct sockaddr*) &serverAddress, sizeof( serverAddress));

// Start listening for connections
w_listen( listenfd, BACKLOG);

// Handle any zombie children by using a signal handler
sa.sa_handler = sigchld_handler;
sigemptyset( &sa.sa_mask);
sa.sa_flags = SA_RESTART;
if( sigaction( SIGCHLD, &sa, NULL) == -1)
{
perror( "signal error");
exit( 1);
}

printf( "Waiting for connections...\n");

while( 1)
{
length = sizeof( clientAddress);
connfd = w_accept( listenfd, ( struct sockaddr*) &clientAddress, &length);
if( connfd < 0)
{
if( errno == EINTR)
{
continue; // back to while
}
else
{
perror( "accept error");
exit( 1);
}
}

printf( "Obtained connection...\n");
childpid = fork();
if ( childpid == 0) /* child process */
{
w_close( listenfd); /* close listening socket */
str_upper( connfd); // process the request
exit( 0);
}

w_close( connfd); /* parent closes connected socket */
}

}

void sigchld_handler( int sig)
{
while( waitpid( -1, NULL, WNOHANG) > 0);
}

void str_upper( int connfd)
{
char buffer[MAXLINE];
while( read( connfd, buffer, MAXLINE - 1) > 0)
{
toUpperCase( buffer, strlen( buffer));
write( connfd, buffer, strlen( buffer));
}
}

void toUpperCase( char buffer[], int length)
{
int i;
for( i = 0; i < length - 1; i++)
{
buffer[i] = toupper( buffer[i]);
}
}

// client
#include "socketwrap.h"

void str_cli( int connfd);

int main( int argc, char** argv)
{
int sockfd;
struct sockaddr_in serverAddress;

if( argc != 2)
{
printf( "Invalid argument count\n");
printf( "Correct usage: tcpcli4 <IPaddress>\n");
exit( 1);
}


sockfd = w_socket( AF_INET, SOCK_STREAM, 0);
bzero( &serverAddress, sizeof( serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons( 11979);
if( inet_pton( AF_INET, argv[1], &serverAddress.sin_addr) <= 0)
{
perror( "inet_pton error");
exit( 1);
}

w_connect( sockfd, ( struct sockaddr*) &serverAddress, sizeof( serverAddress));

str_cli( sockfd);
exit( 0);
}

void str_cli( int connfd)
{
char buffer[MAXLINE];

printf( "Enter the string to echo: ");
while( fgets( buffer, MAXLINE, stdin) > 0)
{

// Send string to echo server, and retrieve response
write( connfd, buffer, strlen( buffer));
read( connfd, buffer, MAXLINE - 1);

// Output echoed string
printf( "Echo response: %s\n", buffer);
printf( "Enter the string to echo: ");
}
}

如果您需要更多信息或有任何不清楚的地方,请告诉我

感谢大家的回复

最佳答案

read() 不会向字符串添加 '\0' 终止符,因此 strlen() 调用的结果是没有明确定义。您需要使用 read() 的返回值(将其存储在变量中,不要只是测试它是否为 > 0 然后将其丢弃)来知道需要多长时间字符串是。

它似乎工作正常一段时间,但这只是运气不好,因为未初始化时缓冲区中存在 '\0'。在读取循环之前尝试使用 memset(buffer, 'X', sizeof buffer) ,您将获得更早的失败。

关于c - unix tcp 客户端/服务器奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12134531/

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