我一直在尝试学习 unix 网络编程,所以我尝试编写一个客户端/服务器程序,其中客户端发送消息,服务器返回转换为大写字母的消息。
当我运行服务器并将客户端连接到我自己的机器并发送一些测试字符串时,它工作正常,直到我之前输入的内容被写入屏幕。
我怀疑它与缓冲区有关。这是我启动服务器后运行的示例:
可以@ubuntu:~$ cd 桌面
可以@ubuntu:~/Desktop$ ./tcpuppcli 127.0.0.1
输入要回显的字符串:test
回声响应:测试
输入要回显的字符串:string2
回声响应:STRING2
输入要回显的字符串:string3
回声响应:STRING3
输入要回显的字符串:aaaaaaaaafsfagd
回声响应:AAAAAAAAAFSFAGD
输入要回显的字符串:gdsgsg
回声响应:GDSGSG
AAFSFAGD ----------->!这是带有来自先前输入的字符的奇怪行!
输入要回显的字符串:^C
可以@ubuntu:~/桌面$
服务端和客户端的代码如下:
// 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: ");
}
}
如果您需要更多信息或有任何不清楚的地方,请告诉我
感谢所有回复