📄 udpdmain.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <dos.h>
#include <time.h>
#include <alloc.h>
#include "tcplib.h"
//=====================================================================
UDPSocket udps;
char udpbuf[520];
int hostport;
int udpl=0;
//=====================================================================
int checkudp(void)
{int len;
udp_kernal(NULL);
if (udpl==0)
{listen_udp(udps,hostport);
udpl=1;
}
if (!peer_udp_data(udps)) return 0;
len=udp_recv(udps,udpbuf,512);
udpbuf[len]=0;
close_udp((UDPSocket)udps);
udpl=0;
return 1;
}
//=====================================================================
int main()
{int rc,sum=0;
char buf[128];
printf("\nUDP SERVER 8888\n");
init_tcplib();
hostport=8888;
udps=new_udpsocket();
listen_udp(udps,hostport);
udpl=1;
while(!kbhit())
{rc=checkudp();
if (rc>0)
{sum++;
printf("Received %d: %s\n",sum,udpbuf);
sprintf(buf,"%d: Hi,How are you ?\r\n",sum);
udp_send_back(udps,hostport,buf,strlen(buf));
}
}
release_udpsocket(udps);
return(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -