环境:VS2013
#include<Winsock2.h>
#include<stdio.h>
#include<stdlib.h>
#pragma comment(lib,"ws2_32.lib")
#define DEFAULT_PORT 8888
#define BUFFER_LENGTH 1024
void main()
{
int iPort = DEFAULT_PORT;
WSADATA wsaData;//协议栈
SOCKET sSocket;//服务端的socket
int iLen;//客户端地址长度
int iSend;//发送数据的长度
int iRecv;//接受数据的缓冲区长度
char sendBuf[] = "Hello! I am server";//服务端要发送的字符串
char recvBuf[BUFFER_LENGTH];//服务器端接受数据的缓冲区
struct sockaddr_in ser, cli; //本地地址和客户地址
printf("-----------------\n");
printf("Server waiting\n");
printf("-----------------\n");
//加载协议栈
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("Failed to load Winsock.\n");
return;
}
//创建服务器端的socket套接口
sSocket = socket(AF_INET, SOCK_DGRAM, 0);
if (sSocket == INVALID_SOCKET)
{
printf("socket(Failed:%d\n)", WSAGetLastError);
return;
}
//初始化服务器端的地址与端口号
ser.sin_family = AF_INET;
ser.sin_port = htons(8888);//服务器端监听的端口号
ser.sin_addr.s_addr = htonl(INADDR_ANY);//服务端的端口地址
//将服务器端socket和服务器的端ip地址和端口号绑定,
if (bind(sSocket, (LPSOCKADDR)&ser, sizeof(ser)) == SOCKET_ERROR)
{
printf("bind() Failed:%d\n", WSAGetLastError());
return;
}
iLen = sizeof(cli);
memset(recvBuf, 0, sizeof(recvBuf));//将接收缓冲区内容全部初始化为0
while (1)
{
//从服务器端套接口中接收数据,并把客户端的地址情况保存到cli上面
iRecv = recvfrom(sSocket, recvBuf, BUFFER_LENGTH, 0,(SOCKADDR*)&cli,&iLen);
if (iRecv == SOCKET_ERROR)
{
printf("recvfrom()Faild;%d\n", WSAGetLastError());
break;
}
else if (iRecv == 0)
{
break;
}
else
{
printf("recvfrom():%s\n", recvBuf);
printf("accepted client ip[%s], port[%d]\n", inet_ntoa(cli.sin_addr), ntohs(cli.sin_port));
}
//向客户端发送数据
iSend = sendto(sSocket, sendBuf, sizeof(sendBuf), 0, (SOCKADDR*)&cli, sizeof(cli));
if (iSend == SOCKET_ERROR)
{
printf("sendto() Faild:%d\n", WSAGetLastError());
printf("----------\n");
break;
}
else if (iSend == 0)
break;
else
{
printf("sendto() succeeded\n");
printf("-=---------\n");
}
}
closesocket(sSocket);//关闭套接口
WSACleanup();//清空协议栈
}
client.c
#include<Winsock2.h>
#include<stdio.h>
#pragma comment(lib,"ws2_32.lib")
void main()
{
WSADATA wsaData;//协议栈
SOCKET sClient;//客户端套接口
struct sockaddr_in ser;//客户端地址
int iPort = 8888;
int iLen;//客户端地址长度
int iSend;
int iRecv;
char sendBuf[1024];//发送数据缓冲区
char recvBuf[1024];//接收数据缓冲区
memset(recvBuf, 0, sizeof(recvBuf));//把接收数据缓冲区初始化为0
//加载协议栈
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("Failed to load Winsock\n");
return;
}
//初始化客户端地址情况
ser.sin_family = AF_INET;
ser.sin_port = htons(8888);
ser.sin_addr.s_addr = inet_addr("127.0.0.1");
//创建客户端套接口
sClient = socket(AF_INET, SOCK_DGRAM, 0);
if (sClient == INVALID_SOCKET)
{
printf("socket() Faild %d\n", WSAGetLastError());
return;
}
iLen = sizeof(ser);
while (1) {
printf_s("请输入要发送的消息:");
gets_s(sendBuf, 1024);
//开始发送数据
iSend = sendto(sClient, sendBuf, sizeof(sendBuf), 0, (struct sockaddr*)&ser, iLen);
if (iSend == SOCKET_ERROR)
{
printf("sendto()faild %d", WSAGetLastError());
return;
}
else if (iSend = 0)
{
return;
}
else {
printf("sendto() succeeded\n");
}
//接收数据
iRecv = recvfrom(sClient, recvBuf, sizeof(recvBuf), 0, (struct sockaddr*)&ser, &iLen);
if (iRecv == SOCKET_ERROR)
{
printf("recvfrom() faild %d\n", WSAGetLastError());
return;
}
else if (iRecv == 0)
return;
else {
printf("recvfrom() %s", recvBuf);
printf("-----------------\n");
}
}
closesocket(sClient);//关闭套接口
WSACleanup();//清空协议栈
}
时间: 2024-12-13 10:08:03