我尝试运行一个简单的客户端和服务器程序。我试图从服务器向服务器发送一条消息,让服务器回复该消息。问题是每次我都会得到10057错误。我不知道为什么。
这是服务器代码
#include <stdio.h>
#include <winsock2.h>
#include <stdlib.h>
#define MYBUFSIZE 128
void shutdown_close(SOCKET s)
{
// Tell the operating system the socket is
// about to be closed
shutdown(s, SD_BOTH);
// close the socket….
closesocket(s);
}
void shutdown_close_and_exit(SOCKET s)
{
shutdown_close(s);
exit(1);
}
void main(int argc, char *argv[])
{
SOCKET srvr_socket;
struct sockaddr_in srvr_addr;
struct sockaddr_in clnt_addr; // Client address
int addr_len = 0;
WSADATA wsaData;
char recv_buf[MYBUFSIZE];
int recv_msg_len;
short portnum;
if (argc == 2)
{
portnum = atoi(argv[1]);
printf("Setting port number to %d\n", portnum);
} else
{
fprintf(stderr, "Usage: %s port_num_to_listen_at\n",
argv[0]);
exit(1);
}
// Init Winsock
if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
{
fprintf(stderr, "Error: WSAStartup() failed");
exit(1);
}
// Create UDP datagram socket for incoming connections
if ((srvr_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == SOCKET_ERROR)
{
fprintf(stderr, "Error: socket() failed with error %d\n", WSAGetLastError());
shutdown_close_and_exit(srvr_socket);
}
// Construct local address structure
memset(&srvr_addr, 0, sizeof(srvr_addr));
srvr_addr.sin_family = AF_INET;
srvr_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Accept messages on any network interface.
srvr_addr.sin_port = htons(portnum); // Bind the port number specified on the command line.
/* Bind to the local address */
if (bind(srvr_socket, (struct sockaddr *) &srvr_addr, sizeof(srvr_addr)) == SOCKET_ERROR)
{
fprintf(stderr, "Error: socket() failed with error %d", WSAGetLastError());
shutdown_close_and_exit(srvr_socket);
}
listen(srvr_socket, SOMAXCONN);
// Loop forever
for (;;)
{
// Set the size of the in-out parameter, where the client address
// and port number will be stored by the OS
addr_len = sizeof(clnt_addr);
if(accept(srvr_socket,(SOCKADDR *) &srvr_addr, &addr_len) == SOCKET_ERROR)
{
fprintf(stderr, "Error: accept() failed with error %d\n", WSAGetLastError());
shutdown_close_and_exit(srvr_socket);
}
// Receive message from client
if ((recv_msg_len = recvfrom(srvr_socket, recv_buf, MYBUFSIZE, 0,
(struct sockaddr *) &clnt_addr, &addr_len )) == SOCKET_ERROR) {
fprintf(stderr, "Error: recvfrom() failed with error %d\n", WSAGetLastError());
shutdown_close_and_exit(srvr_socket);
} else {
printf("Received message of size %d: %s\n from client %s:%d\n",
recv_msg_len, recv_buf, inet_ntoa(clnt_addr.sin_addr),
ntohs(clnt_addr.sin_port));
}
// 'echo' message back to client
if ((recv_msg_len = sendto(srvr_socket, recv_buf, recv_msg_len, 0,
(struct sockaddr *) &clnt_addr, addr_len )) == SOCKET_ERROR) {
fprintf(stderr, "Error: sendto() failed with error %d\n", WSAGetLastError());
shutdown_close_and_exit(srvr_socket);
} else {
printf("Echo'd message %s to client %s:%d\n",
recv_buf, inet_ntoa(clnt_addr.sin_addr),
ntohs(clnt_addr.sin_port));
}
}
}
这是客户端代码:
#include <stdio.h>
#include <winsock2.h>
#include <stdlib.h>
#define BUFSIZE 128 /* Size of receive buffer */
void shutdown_close(SOCKET s)
{
// Tell the operating system the socket is
// about to be closed
shutdown(s, SD_BOTH);
// close the socket….
closesocket(s);
}
void shutdown_close_and_exit(SOCKET s)
{
shutdown_close(s);
exit(1);
}
void main(int argc, char *argv[])
{
SOCKET servSock;
struct sockaddr_in srvr_addr;
struct sockaddr_in recv_addr;
int addr_len = 0;
WSADATA wsaData;
char mesg_buf[BUFSIZE];
char *mesg_to_send;
long server_IP;
short portnum;
if (argc == 4)
{
portnum = atoi(argv[1]);
printf("Setting port number to %d\n", portnum);
server_IP = inet_addr(argv[2]);
printf("Target server IP address is %s\n", argv[2]);
mesg_to_send = argv[3];
} else
{
fprintf(stderr, "Usage: %s server_port_num server_IP_address message_to_send\n",
argv[0]);
exit(1);
}
// Init Winsock
if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
{
fprintf(stderr, "Error: WSAStartup() failed with error %d\n", WSAGetLastError());
exit(1);
}
// Create socket for incoming connections
if ((servSock = socket(AF_INET,SOCK_STREAM, IPPROTO_TCP)) == SOCKET_ERROR)
{
fprintf(stderr, "Error: socket() failed with error %d\n", WSAGetLastError());
shutdown_close_and_exit(servSock);
}
// Construct local address structure
// with server address. This is like
// addressing the envelope of a letter.
memset(&srvr_addr, 0, sizeof(srvr_addr));
srvr_addr.sin_family = AF_INET;
srvr_addr.sin_addr.s_addr = server_IP;
srvr_addr.sin_port = htons(portnum);
if ( connect(servSock, (SOCKADDR*) &srvr_addr, sizeof(srvr_addr)) == SOCKET_ERROR)
{
fprintf(stderr, "Error: connect() failed with error %d\n", WSAGetLastError());
shutdown_close_and_exit(servSock);
}
// Set the size of the in-out parameter
addr_len = sizeof(recv_addr);
if (sendto(servSock, mesg_to_send, strlen(mesg_to_send) + 1, 0,
(sockaddr *) &srvr_addr, addr_len) == SOCKET_ERROR)
{
fprintf(stderr, "Error: sendto() failed with error %d\n", WSAGetLastError());
shutdown_close_and_exit(servSock);
}
printf("Send message %s to server at %s:%d\n", argv[3], argv[2], portnum);
// Sleep 1 full second to allow message to get to server and be sent back...
Sleep(1000);
addr_len = sizeof(recv_addr);
if (recvfrom(servSock, mesg_buf, BUFSIZE, 0,
(sockaddr *) &recv_addr, &addr_len) == SOCKET_ERROR)
{
fprintf(stderr, "Error: recvfrom() failed with error %d\n", WSAGetLastError());
shutdown_close_and_exit(servSock);
}
printf("Received message %s from %s:%d\n", mesg_buf,
inet_ntoa(recv_addr.sin_addr), ntohs(recv_addr.sin_port));
// close socket gracefully
shutdown_close(servSock);
}
任何人都可以看到代码有什么问题吗?
答案 0 :(得分:1)
您应该recv
用于SOCK_STREAM
套接字,而不是recvfrom
。
(但这是一堆巨大的代码,并没有明显指出失败的原因,所以除此之外还有其他问题。)
答案 1 :(得分:1)
使用send()
代替sendto()
,recv()
代替recvfrom()
。此外,您在服务器端将错误的sockaddr_in
传递给accept()
。
答案 2 :(得分:0)
我弄清楚出了什么问题。我的服务器从未建立连接,因为我从未与accept函数建立连接。换句话说,我接受了一个连接,但没有记住它。我应该创建一个接受连接的套接字,并将其设置为接受函数,如下所示:
Socket acceptSocket;
...
AcceptSocket = accept(srvr_socket,(SOCKADDR *) &clnt_addr, &addr_len) ;
if(AcceptSocket == INVALID_SOCKET)
{
fprintf(stderr, "Error: accept() failed with error %d\n", WSAGetLastError());
shutdown_close_and_exit(srvr_socket);
}
另外我应该使用recv()和send()而不是像RichieHindle那样提到的sendto()和recvfrom(),因为它们是为UDP无连接套接字而不是TCP连接套接字而设计的
答案 3 :(得分:0)
当我从recvfrom()收到错误10057时,我的初始代码段如下:
if (listen(s, SOMAXCONN) == SOCKET_ERROR) {
printf("Error in Listening .. ErroR Code: %d \n", WSAGetLastError());
exit(EXIT_FAILURE);
}
else
printf("Listening\n");
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
new_socket = accept(s, (struct sockaddr *)&client, &c);
if (new_socket == INVALID_SOCKET)
{
printf("accept failed with error code : %d", WSAGetLastError());
return 1;
}
else
puts("Connection accepted");
recv_size = recvfrom(s , client_msg, sizeof(client_msg), 0,
(SOCKADDR *)&recv_from, &addrlen);
我试图从我正在侦听的同一个套接字上接收来自客户端的数据,即“s”而不是我接受客户端连接的新套接字,即“new_socket”。
After making the following changes:
recv_size = recvfrom(new_socket , client_msg, sizeof(client_msg), 0, (SOCKADDR
*)&recv_from, &addrlen);
我可以完美地从客户端应用程序接收数据。