先看Server端的代码:
[code]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock.h>
const int MYPORT=4950; // the port users will be connecting to
const int MAXBUFLEN=100;
void init_win_socket();
void clean_win_socket();
void close_win_socket(SOCKET &soc_handle);
//this socket is intend to use as a simple server
int main()
{
puts("start main server....");
//init windows socket
init_win_socket();
SOCKET main_sock_fd;
struct sockaddr_in main_soc_addr; // my address information
struct sockaddr_in client_soc_addr; // connector's address information
int addr_len;
int numbytes;
char buf[MAXBUFLEN];
int ret=0;
//create the sock
if ((main_sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
puts("CREATE SOCK ERROR:");
}
//configure sockaddr_in
main_soc_addr.sin_family = AF_INET; // host byte order
main_soc_addr.sin_port = htons(MYPORT); // short, network byte order
main_soc_addr.sin_addr.s_addr= htonl(INADDR_ANY); // automatically fill with my IP
memset(main_soc_addr.sin_zero, '\0', sizeof (main_soc_addr.sin_zero));
//bind the socket
ret = bind(main_sock_fd, (struct sockaddr *)&main_soc_addr, sizeof main_soc_addr);
if ( ret == -1)
{
puts("BIND SOCK ERROR:");
}
//get data from client socket
addr_len = sizeof client_soc_addr;
numbytes = recvfrom(main_sock_fd, buf, MAXBUFLEN-1 , 0,
(struct sockaddr *)&client_soc_addr, &addr_len);
if (numbytes == -1)
{
puts("RECEIVE SOCK ERROR:");
}
else
{
printf("got packet from %s\n",inet_ntoa(client_soc_addr.sin_addr));
printf("packet is %d bytes long\n",numbytes);
buf[numbytes] = '\0';
printf("packet contains \"%s\"\n",buf);
}
//close and clean the sockets
close_win_socket(main_sock_fd);
clean_win_socket();
puts("any key to continue...");
getchar();
return 0;
}
void init_win_socket()
{
WSADATA wdata;
if(WSAStartup(MAKEWORD(2,2), &wdata)!=0)
{
puts("WAS ERROR:");
}
}
void close_win_socket(SOCKET &soc_handle)
{
if(soc_handle>0)
{
closesocket(soc_handle);
}
}
void clean_win_socket()
{
WSACleanup();
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock.h>
const int SERVERPORT= 4950; // the port users will be connecting to
const int MYPORT=4900; // the local port
#define SERVER_IP "192.168.136.93"
void init_win_socket();
void clean_win_socket();
void close_win_socket(SOCKET &soc_handle);
//this socket is intend to use as a simple client
int main()
{
puts("start main client...");
int numBytes=0;
int ret=0;
char sendBuf[]="Hello, this is used to test UDP socket!\0";
//init windows socket
init_win_socket();
SOCKET local_sock_fd;
struct sockaddr_in main_soc_addr;
struct sockaddr_in client_soc_addr;
//create the client socket
local_sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if(local_sock_fd ==-1)
{
puts("CREATE SOCK ERROR:");
}
//configure the client's local socket addr
client_soc_addr.sin_family = AF_INET;
client_soc_addr.sin_port = htons(MYPORT);
client_soc_addr.sin_addr.s_addr = htonl(INADDR_ANY);
memset(client_soc_addr.sin_zero, '\0', sizeof(client_soc_addr.sin_zero));
//bind the socket
ret = bind(local_sock_fd, (struct sockaddr *)&client_soc_addr, sizeof(client_soc_addr));
if(ret==-1)
{
puts("BIND SOCK ERROR:");
}
//configure the client's server socket addr
main_soc_addr.sin_family = AF_INET;
main_soc_addr.sin_port = htons(SERVERPORT);
main_soc_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
memset(main_soc_addr.sin_zero, '\0', sizeof(main_soc_addr.sin_zero));
//now,we can send data to the server
numBytes =sendto(local_sock_fd, sendBuf, strlen(sendBuf),
0, (struct sockaddr*)&main_soc_addr, sizeof(sockaddr));
if(numBytes ==-1)
{
puts("SEND DATA ERROR:");
}
//close and clean the windows sockets
close_win_socket(local_sock_fd);
clean_win_socket();
puts("any key to continue...");
getchar();
return 0;
}
//init windows socket
void init_win_socket()
{
WSADATA wdata;
if(WSAStartup(MAKEWORD(2,2), &wdata)!=0)
{
puts("WAS ERROR:");
}
}
//close windows socket
void close_win_socket(SOCKET &soc_handle)
{
if(soc_handle>0)
{
closesocket(soc_handle);
}
}
//clean windows socket
void clean_win_socket()
{
WSACleanup();
}
#include <stdlib.h>
#include <string.h>
#include <winsock.h>
const int SERVERPORT= 4950; // the port users will be connecting to
const int MYPORT=4900; // the local port
#define SERVER_IP "192.168.136.93"
void init_win_socket();
void clean_win_socket();
void close_win_socket(SOCKET &soc_handle);
//this socket is intend to use as a simple client
int main()
{
puts("start main client...");
int numBytes=0;
int ret=0;
char sendBuf[]="Hello, this is used to test UDP socket!\0";
//init windows socket
init_win_socket();
SOCKET local_sock_fd;
struct sockaddr_in main_soc_addr;
struct sockaddr_in client_soc_addr;
//create the client socket
local_sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if(local_sock_fd ==-1)
{
puts("CREATE SOCK ERROR:");
}
//configure the client's local socket addr
client_soc_addr.sin_family = AF_INET;
client_soc_addr.sin_port = htons(MYPORT);
client_soc_addr.sin_addr.s_addr = htonl(INADDR_ANY);
memset(client_soc_addr.sin_zero, '\0', sizeof(client_soc_addr.sin_zero));
//bind the socket
ret = bind(local_sock_fd, (struct sockaddr *)&client_soc_addr, sizeof(client_soc_addr));
if(ret==-1)
{
puts("BIND SOCK ERROR:");
}
//configure the client's server socket addr
main_soc_addr.sin_family = AF_INET;
main_soc_addr.sin_port = htons(SERVERPORT);
main_soc_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
memset(main_soc_addr.sin_zero, '\0', sizeof(main_soc_addr.sin_zero));
//now,we can send data to the server
numBytes =sendto(local_sock_fd, sendBuf, strlen(sendBuf),
0, (struct sockaddr*)&main_soc_addr, sizeof(sockaddr));
if(numBytes ==-1)
{
puts("SEND DATA ERROR:");
}
//close and clean the windows sockets
close_win_socket(local_sock_fd);
clean_win_socket();
puts("any key to continue...");
getchar();
return 0;
}
//init windows socket
void init_win_socket()
{
WSADATA wdata;
if(WSAStartup(MAKEWORD(2,2), &wdata)!=0)
{
puts("WAS ERROR:");
}
}
//close windows socket
void close_win_socket(SOCKET &soc_handle)
{
if(soc_handle>0)
{
closesocket(soc_handle);
}
}
//clean windows socket
void clean_win_socket()
{
WSACleanup();
}
转载于:https://blog.51cto.com/gghhgame51333/86545