tcp的echo 程序很多,udp 很难找。
我把收集到的源码 发布出来。
linux 服务器
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#define SERVER_PORT 8888
#define MAX_MSG_SIZE 1024
void udps_respon(int sockfd) {
struct sockaddr_in addr;
unsigned int addrlen;
int n;
char msg[MAX_MSG_SIZE];
char show[MAX_MSG_SIZE+100];
while(1) {
// 从网络上读,写到网络上去
// 初始化addr长度!
addrlen = sizeof(addr);
if((n = recvfrom(sockfd,msg,MAX_MSG_SIZE,0,
(struct sockaddr *)&addr,&addrlen)) < 0) {
fprintf(stderr,"Recveive packahe error:%s",
strerror(errno));
continue;
}
msg[n] ='/0';//原来是msg[n-1] ='/0';
// 显示服务器端已经收到了信息
snprintf(show,MAX_MSG_SIZE+100,"I have received %s from %sn",msg,
inet_ntoa(addr.sin_addr));
sendto(sockfd,msg,n,0,(struct sockaddr *)&addr,addrlen);
printf("%s",show);
}
}
int main(void) {
int sockfd;
struct sockaddr_in addr;
sockfd = socket(AF_INET,SOCK_DGRAM,0);
if(sockfd < 0) {
fprintf(stderr,"Socket Error:%sn",strerror(errno));
exit(1);
}
bzero(&addr,sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(SERVER_PORT);
if(bind(sockfd,(struct sockaddr *)&addr,
sizeof(struct sockaddr_in)) < 0) {
fprintf(stderr,"Bind Error:%sn",strerror(errno));
exit(1);
}
udps_respon(sockfd);
close(sockfd);
exit(1);
}
c++ 源码
#include <iostream>
#include <cstdlib>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
using namespace std;
int main(int argc, char *argv[])
{
int s;
char buffer[10];
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0)
{
cout << "socket() error!" << endl;
}
sockaddr_in addr;
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(5555);
if (bind(s, (sockaddr*)(&addr), sizeof(addr)))
{
cout << "bind() error!" << endl;
}
while (1)
{
int recvSize;
sockaddr_in recvAddr;
socklen_t recvAddrLen = sizeof(recvAddr);
bzero(&recvAddr, sizeof(recvAddr));
recvSize = recvfrom(s, buffer, sizeof(buffer), 0, (sockaddr*)(&recvAddr), &recvAddrLen);
if (recvSize < 0)
{
cout << "recvfrom() error!" << endl;
continue;
}
int sendSize;
sendSize = sendto(s, buffer, sizeof(buffer), 0, (sockaddr*)(&recvAddr), sizeof(recvAddr));
if (sendSize < 0)
{
cout << "sendto() error!" << endl;
continue;
}
cout << buffer << endl;
}
return EXIT_SUCCESS;
}
linux 客户端 可以移植到windows下
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define MAX_BUF_SIZE 1024
void udpc_request(int sockfd,const struct sockaddr_in *addr,int len) {
char buffer[MAX_BUF_SIZE];
int n;
while(1) {
// 从键盘读入,写到服务端
fgets(buffer,MAX_BUF_SIZE,stdin);
sendto(sockfd,buffer,strlen(buffer),0,(struct sockaddr *)addr,len);
bzero(buffer,MAX_BUF_SIZE);
// 从网络上读,写到屏幕上
n = recvfrom(sockfd,buffer,MAX_BUF_SIZE,0,NULL,NULL);
msg[n] ='/0';//原来是msg[n-1] ='/0';
fprintf(stdout,"I received %s from server.n",buffer);
}
}
int main(int argc,char **argv) {
int sockfd,port;
struct sockaddr_in addr;
if(argc != 3) {
fprintf(stderr,"Usage:%s server_ip server_portn",argv[0]);
exit(1);
}
if((port = atoi(argv[2])) < 0) {
fprintf(stderr,"Usage:%s server_ip server_portn",argv[0]);
exit(1);
}
sockfd = socket(AF_INET,SOCK_DGRAM,0);
if(sockfd < 0) {
fprintf(stderr,"Socket Error:%sn",strerror(errno));
exit(1);
}
// 填充服务端的资料
bzero(&addr,sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
if(inet_aton(argv[1],&addr.sin_addr) < 0) {
fprintf(stderr,"IP error:%sn",strerror(errno));
exit(1);
}
udpc_request(sockfd,&addr,sizeof(struct sockaddr_in));
close(sockfd);
exit(0);
}
c#的客户端
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace DefaultNamespace
{
/// <summary>
/// Description of Class1.
/// </summary>
public class UdpEchoClient
{
private static readonly int BUF_SIZE=2048;
public static void Main(){
Socket clientSocket=new Socket(AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp
);
clientSocket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout,
3000
);
EndPoint serverEndPoint=new IPEndPoint(IPAddress.Parse("127.0.0.1"),
8004
);
Console.WriteLine("echo client start");
while(true){
string input=Console.ReadLine();
if("exit"==input.ToLower()){
clientSocket.Close();
Console.WriteLine("echo client stop.");
break;
}
byte[] buf=Encoding.ASCII.GetBytes(input);
clientSocket.SendTo(buf,SocketFlags.None,serverEndPoint);
buf=new byte[BUF_SIZE];
EndPoint remoteEndPoint=new IPEndPoint(IPAddress.Any,0);
try{
int receive=clientSocket.ReceiveFrom(buf,ref remoteEndPoint);
string strReceive=Encoding.ASCII.GetString(buf,0,receive);
Console.WriteLine("echoed from server: {0} message:{1}",remoteEndPoint,strReceive);
}catch(SocketException e){
Console.WriteLine(e.Message);
}
}//while
}
}
}
所有程序来源于互联网,没有应用于启明星辰任何产品。