能否给我一个用纯C编写的UDP发送和接收的程序

不要C++的,要纯C编写的。辛苦各位了不是TCP啊,要UDP的
2024-11-28 21:59:14
推荐回答(5个)
回答(1):

UDP的,你看下

1.服务器端实现

程序在收到客户端发送来的消息后,给客户端发送消息,提示客户端收到了该消息

#include
#include
#include
#include
#include
#include

int main(int argc, char *argv[])
{
int sock, length, fromlen, n;
struct sockaddr_in server;
struct sockaddr_in from;
char buf[1024];

//要求执行是输入端口信息
if (argc!= 2) {
printf( "Usage: %s port_num\n",argv[0]);
return 1;
}

//创建通信所需的套接字,并与地址和端口信息帮定
sock=socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0){
perror("cannot create communicating socket");
return 1;
}

length = sizeof(server);
bzero(&server,length);
server.sin_family=AF_INET;
server.sin_addr.s_addr=INADDR_ANY;
server.sin_port=htons(atoi(argv[1]));

if (bind(sock,(struct sockaddr *)&server,length)<0){
perror("cannot bind the socket");
close(sock);
return 1;
}

fromlen = sizeof(struct sockaddr_in);

//读取客户端发送来的信息,显示后,发回相关信息给客户端
while (1) {
n = recvfrom(sock,buf,sizeof(buf),0,(struct sockaddr *)&from,&fromlen);
if (n < 0) {
perror("cannot receive date from client");
break;
}
write(STDOUT_FILENO,"server: Received a datagram: ",29);
write(STDOUT_FILENO,buf,n);

n = sendto(sock,"send message to client\n",22,
0,(struct sockaddr *)&from,fromlen);
if (n < 0) {
perror("cannot send data to the client");
break;
}
}

close(sock);
return 0;
}

2.客户端实现

#include
#include
#include
#include
#include
#include
#include

int main(int argc, char *argv[])
{
int sock, length, n;
struct sockaddr_in server, from;
struct hostent *hp;
char buffer[256];

//判断输入参数是否符合要求
if (argc != 3) {
printf("Usage: %s server_ip port_num\n",argv[0]);
return 1;
}

//创建通信套接字
sock= socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
perror("cannot create communicating socket");
return 1;
}

server.sin_family = AF_INET;
hp = gethostbyname(argv[1]);
if (hp==0) {
perror("cannot get the server ip address");
return 1;
}

bcopy((char *)hp->h_addr,
(char *)&server.sin_addr,
hp->h_length);

server.sin_port = htons(atoi(argv[2]));
length=sizeof(struct sockaddr_in);

printf("(client) enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);

//发送数据给指定服务器
n=sendto(sock,buffer,strlen(buffer),0,&server,length);
if (n < 0){
perror("cannot get message from the client");
return 1;
}

//从服务器中接受数据
bzero(buffer,256);
n = recvfrom(sock,buffer,256,0,&from, &length);
if (n < 0) {
perror("cannot send message to the server");
return 1;
}

printf("client got message : %s\n",buffer);
close(sock);
return 0;
}

回答(2):

纯C的???

这个够纯
//////////////////////////////////////////////////
// TCPServer.cpp文件

#include "../common/InitSock.h"
#include

CInitSock(BYTE minorVer, BYTE majorVer)
{
BYTE minorVer = 2, majorVer = 2
// 初始化WS2_32.dll
WSADATA wsaData;
WORD sockVersion = MAKEWORD(minorVer, majorVer);
if(::WSAStartup(sockVersion, &wsaData) != 0)
{
exit(0);
}
}

int main()
{
// 创建套节字
SOCKET sListen = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sListen == INVALID_SOCKET)
{
printf("Failed socket() \n");
return 0;
}

// 填充sockaddr_in结构
sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(4567);
sin.sin_addr.S_un.S_addr = INADDR_ANY;

// 绑定这个套节字到一个本地地址
if(::bind(sListen, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR)
{
printf("Failed bind() \n");
return 0;
}

// 进入监听模式
if(::listen(sListen, 2) == SOCKET_ERROR)
{
printf("Failed listen() \n");
return 0;
}

// 循环接受客户的连接请求
sockaddr_in remoteAddr;
int nAddrLen = sizeof(remoteAddr);
SOCKET sClient;
char szText[] = " TCP Server Demo! \r\n";
while(TRUE)
{
// 接受一个新连接
sClient = ::accept(sListen, (SOCKADDR*)&remoteAddr, &nAddrLen);
if(sClient == INVALID_SOCKET)
{
printf("Failed accept()");
continue;
}

printf(" 接受到一个连接:%s \r\n", inet_ntoa(remoteAddr.sin_addr));

// 向客户端发送数据
::send(sClient, szText, strlen(szText), 0);
// 关闭同客户端的连接
::closesocket(sClient);
}

// 关闭监听套节字
::closesocket(sListen);

return 0;
}

回答(3):

我看热闹!

回答(4):

我也正在学习UDP
论坛上有很多源程序,建议多在百度上搜搜

回答(5):

那个头文件?