我有在Linux上运行的UDPServer程序和在Windows 10上运行的UDPClient程序。
注意:在UDP客户端程序中,您必须在哈希定义中添加linux服务器IP。
在Windows上进行编译:gcc -Wall -o UDPClient UDPClient.c -lws2_32
问题 UDPServer从UDP客户端接收消息,然后回复。但是Windows上的UDPClient不会从Windows接收消息。
UDPServer.c(在Linux上)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define PORT 6200
#define MAXLINE 1024
int main() {
int sockfd;
char buffer[MAXLINE];
char *hello = "Hello from server";
struct sockaddr_in servaddr, cliaddr;
int n;
socklen_t from_len = sizeof(struct sockaddr);
socklen_t to_len = sizeof(struct sockaddr);
// Creating socket file descriptor
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
memset(&cliaddr, 0, sizeof(cliaddr));
// Filling server information
servaddr.sin_family = AF_INET; // IPv4
//servaddr.sin_addr.s_addr = inet_addr(IP_ADDR); ;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(PORT);
// Bind the socket with the server address
if ( bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0 )
{
perror("bind failed");
exit(EXIT_FAILURE);
}
n = recvfrom(sockfd, (char *)buffer, MAXLINE, 0, ( struct sockaddr *) &cliaddr, &from_len);
buffer[n] = '\0';
printf("Client : %s ; IP = %s ; Port = %d\n", buffer,inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port));
sendto(sockfd, (const char *)hello, strlen(hello), MSG_CONFIRM, (const struct sockaddr *) &cliaddr, to_len);
printf("Hello message sent.\n");
return 0;
}
// UDP client (on windows)
#include<stdio.h>
#include<winsock2.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>
#define PORT 6200
#define IP_ADDR "xx.xx.xx.xx" //Add the linux server IP
#define BUF_SIZE 1024
/* Compilation: gcc -Wall -o UDPClient UDPClient.c -lws2_32 */
int SetupWinSock()
{
WSADATA wsa;
//printf("SetupWinSock: Initializing socket system\n");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
printf("SetupWinSock: Failed. Error Code : %d",WSAGetLastError());
return -1;
}
//printf("SetupWinSock: Initialization complete\n");
return 0;
}
SOCKET CreateUDPSocket()
{
SOCKET udpSocket;
if((udpSocket = socket(AF_INET , SOCK_DGRAM , IPPROTO_UDP )) == INVALID_SOCKET)
{
printf("CreateUDPSocket: Failed to create socket : %d" , WSAGetLastError());
return -1;
}
else
{
//printf("CreateUDPSocket: Socket creation successful\n");
}
return udpSocket;
}
int main() {
char buffer[BUF_SIZE];
char *hello = "Hello from client";
struct sockaddr_in servaddr, localAddr;
SOCKET udpSocket;
int n, len;
SetupWinSock();
// Creating socket file descriptor
udpSocket = CreateUDPSocket();
memset(&servaddr, 0, sizeof(servaddr));
// Server info
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr.s_addr = inet_addr(IP_ADDR);
// Client info
localAddr.sin_family = AF_INET;
localAddr.sin_port = htons(PORT);
localAddr.sin_addr.s_addr = INADDR_ANY;
if ( bind(udpSocket,(const struct sockaddr *)&localAddr, sizeof(localAddr)) < 0 )
{
printf("Failed to bind\n");
return -1;
}
sendto(udpSocket, (const char *)hello, strlen(hello), 0, (const struct sockaddr *) &servaddr, sizeof(servaddr));
printf("Hello message sent.\n");
n = recvfrom(udpSocket, (char *)buffer, BUF_SIZE, 0, (struct sockaddr *) &servaddr, &len);
buffer[n] = '\0';
printf("Server : %s\n", buffer);
fflush(stdout);
closesocket(udpSocket);
WSACleanup();
while(1);
return 0;
}