c 套接字多次 send/recv 调用
c sockets multiple send/recv calls
我正在学习 C 套接字编程。我对此很陌生,所以如果答案看起来很明显,请原谅我。所以基本上我希望客户端向服务器发送两个字符串(用户名和密码)。问题是服务器只收到用户名然后关闭连接。
谁能解释为什么会这样,我怎样才能让它按预期工作?
客户:
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <signal.h>
#include <ctype.h>
#include <arpa/inet.h>
#include <netdb.h>
#define PORT 20000
#define LENGTH 512
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
/* Variable Definition */
int sockfd;
int nsockfd;
char revbuf[LENGTH];
struct sockaddr_in remote_addr;
/* Get the Socket file descriptor */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
fprintf(stderr, "ERROR: Failed to obtain Socket Descriptor! (errno = %d)\n",errno);
exit(1);
}
/* Fill the socket address struct */
remote_addr.sin_family = AF_INET;
remote_addr.sin_port = htons(PORT);
inet_pton(AF_INET, "127.0.0.1", &remote_addr.sin_addr);
bzero(&(remote_addr.sin_zero), 8);
/* Try to connect the remote */
if (connect(sockfd, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr)) == -1)
{
fprintf(stderr, "ERROR: Failed to connect to the host! (errno = %d)\n",errno);
exit(1);
}
else
printf("[Client] Connected to server at port %d...ok!\n", PORT);
char username[20], pw[20],buff;
int rc,rc2;
printf("username: ");
scanf("%s",username);
rc=send(sockfd, username, strlen(username)+1, 0);
printf("\n");
printf("password: ");
scanf("%s",pw);
rc2=send(sockfd, pw, strlen(pw)+1, 0);
close (sockfd);
printf("[Client] Connection lost.\n");
return (0);
}
服务器:
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <signal.h>
#include <ctype.h>
#include <arpa/inet.h>
#include <netdb.h>
#define PORT 20000
#define BACKLOG 5
#define LENGTH 512
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main ()
{
/* Defining Variables */
int sockfd;
int nsockfd;
int num;
int sin_size;
struct sockaddr_in addr_local; /* client addr */
struct sockaddr_in addr_remote; /* server addr */
char revbuf[LENGTH]; // Receiver buffer
/* Get the Socket file descriptor */
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
{
fprintf(stderr, "ERROR: Failed to obtain Socket Descriptor. (errno = %d)\n", errno);
exit(1);
}
else
printf("[Server] Obtaining socket descriptor successfully.\n");
/* Fill the client socket address struct */
addr_local.sin_family = AF_INET; // Protocol Family
addr_local.sin_port = htons(PORT); // Port number
addr_local.sin_addr.s_addr = INADDR_ANY; // AutoFill local address
bzero(&(addr_local.sin_zero), 8); // Flush the rest of struct
/* Bind a special Port */
if( bind(sockfd, (struct sockaddr*)&addr_local, sizeof(struct sockaddr)) == -1 )
{
fprintf(stderr, "ERROR: Failed to bind Port. (errno = %d)\n", errno);
exit(1);
}
else
printf("[Server] Binded tcp port %d in addr 127.0.0.1 sucessfully.\n",PORT);
/* Listen remote connect/calling */
if(listen(sockfd,BACKLOG) == -1)
{
fprintf(stderr, "ERROR: Failed to listen Port. (errno = %d)\n", errno);
exit(1);
}
else
printf ("[Server] Listening the port %d successfully.\n", PORT);
int success = 0;
while(1)
{
sin_size = sizeof(struct sockaddr_in);
if ((nsockfd = accept(sockfd, (struct sockaddr *)&addr_remote, &sin_size)) == -1)
{
fprintf(stderr, "ERROR: Obtaining new Socket Despcritor. (errno = %d)\n", errno);
exit(1);
}
else
printf("[Server] Server has got connected from %s.\n", inet_ntoa(addr_remote.sin_addr));
int rc,rc2;
char username[20],pw[20];
rc = recv(nsockfd, username, strlen(username), 0);
printf(username);
rc2 = recv(nsockfd, pw, strlen(pw), 0);
printf("\n");
printf(pw);
success = 1;
close(nsockfd);
printf("[Server] Connection with Client closed. Server will wait now...\n");
// while(waitpid(-1, NULL, WNOHANG) > 0);
//}
}
}
在服务器中:
recv(nsockfd, username, strlen(username), 0);
.
第三个参数必须指定缓冲区的大小。但是,username
还不包含任何数据,因此 strlen(username)
是未定义的行为,很可能 returns 是不合适的。请改用 sizeof(username)
。密码也一样。
另请注意,recv
将从流中读取最多 20 个字节。因此,除非保证 username
始终为 19 个字符 + '[=16=]'
,否则第一个 recv
可能(有点意外)也从 password
中读取一些字符,从第二 recv
。 TCP 流中没有消息边界。
我正在学习 C 套接字编程。我对此很陌生,所以如果答案看起来很明显,请原谅我。所以基本上我希望客户端向服务器发送两个字符串(用户名和密码)。问题是服务器只收到用户名然后关闭连接。
谁能解释为什么会这样,我怎样才能让它按预期工作?
客户:
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <signal.h>
#include <ctype.h>
#include <arpa/inet.h>
#include <netdb.h>
#define PORT 20000
#define LENGTH 512
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
/* Variable Definition */
int sockfd;
int nsockfd;
char revbuf[LENGTH];
struct sockaddr_in remote_addr;
/* Get the Socket file descriptor */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
fprintf(stderr, "ERROR: Failed to obtain Socket Descriptor! (errno = %d)\n",errno);
exit(1);
}
/* Fill the socket address struct */
remote_addr.sin_family = AF_INET;
remote_addr.sin_port = htons(PORT);
inet_pton(AF_INET, "127.0.0.1", &remote_addr.sin_addr);
bzero(&(remote_addr.sin_zero), 8);
/* Try to connect the remote */
if (connect(sockfd, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr)) == -1)
{
fprintf(stderr, "ERROR: Failed to connect to the host! (errno = %d)\n",errno);
exit(1);
}
else
printf("[Client] Connected to server at port %d...ok!\n", PORT);
char username[20], pw[20],buff;
int rc,rc2;
printf("username: ");
scanf("%s",username);
rc=send(sockfd, username, strlen(username)+1, 0);
printf("\n");
printf("password: ");
scanf("%s",pw);
rc2=send(sockfd, pw, strlen(pw)+1, 0);
close (sockfd);
printf("[Client] Connection lost.\n");
return (0);
}
服务器:
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <signal.h>
#include <ctype.h>
#include <arpa/inet.h>
#include <netdb.h>
#define PORT 20000
#define BACKLOG 5
#define LENGTH 512
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main ()
{
/* Defining Variables */
int sockfd;
int nsockfd;
int num;
int sin_size;
struct sockaddr_in addr_local; /* client addr */
struct sockaddr_in addr_remote; /* server addr */
char revbuf[LENGTH]; // Receiver buffer
/* Get the Socket file descriptor */
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
{
fprintf(stderr, "ERROR: Failed to obtain Socket Descriptor. (errno = %d)\n", errno);
exit(1);
}
else
printf("[Server] Obtaining socket descriptor successfully.\n");
/* Fill the client socket address struct */
addr_local.sin_family = AF_INET; // Protocol Family
addr_local.sin_port = htons(PORT); // Port number
addr_local.sin_addr.s_addr = INADDR_ANY; // AutoFill local address
bzero(&(addr_local.sin_zero), 8); // Flush the rest of struct
/* Bind a special Port */
if( bind(sockfd, (struct sockaddr*)&addr_local, sizeof(struct sockaddr)) == -1 )
{
fprintf(stderr, "ERROR: Failed to bind Port. (errno = %d)\n", errno);
exit(1);
}
else
printf("[Server] Binded tcp port %d in addr 127.0.0.1 sucessfully.\n",PORT);
/* Listen remote connect/calling */
if(listen(sockfd,BACKLOG) == -1)
{
fprintf(stderr, "ERROR: Failed to listen Port. (errno = %d)\n", errno);
exit(1);
}
else
printf ("[Server] Listening the port %d successfully.\n", PORT);
int success = 0;
while(1)
{
sin_size = sizeof(struct sockaddr_in);
if ((nsockfd = accept(sockfd, (struct sockaddr *)&addr_remote, &sin_size)) == -1)
{
fprintf(stderr, "ERROR: Obtaining new Socket Despcritor. (errno = %d)\n", errno);
exit(1);
}
else
printf("[Server] Server has got connected from %s.\n", inet_ntoa(addr_remote.sin_addr));
int rc,rc2;
char username[20],pw[20];
rc = recv(nsockfd, username, strlen(username), 0);
printf(username);
rc2 = recv(nsockfd, pw, strlen(pw), 0);
printf("\n");
printf(pw);
success = 1;
close(nsockfd);
printf("[Server] Connection with Client closed. Server will wait now...\n");
// while(waitpid(-1, NULL, WNOHANG) > 0);
//}
}
}
在服务器中:
recv(nsockfd, username, strlen(username), 0);
.
第三个参数必须指定缓冲区的大小。但是,username
还不包含任何数据,因此 strlen(username)
是未定义的行为,很可能 returns 是不合适的。请改用 sizeof(username)
。密码也一样。
另请注意,recv
将从流中读取最多 20 个字节。因此,除非保证 username
始终为 19 个字符 + '[=16=]'
,否则第一个 recv
可能(有点意外)也从 password
中读取一些字符,从第二 recv
。 TCP 流中没有消息边界。