Linux 循环服务器之UDP

流程

  1. 创建套接字:s
  2. 设置地址并绑定
  3. while
    [1] recv
    [2] send
  4. close(s)

客户端

  1. 使用telnet测试

Code

#include 

#include 
#include 
#include 
#include 

#include 
#include 

#include 
using std::cout;

const unsigned short port=8888;

int Socket(){
    int s=socket(AF_INET,SOCK_DGRAM,0);
    
    struct sockaddr_in addr;
    bzero(&addr,sizeof(addr));
    addr.sin_family=AF_INET;
    addr.sin_port=htons(port);
    addr.sin_addr.s_addr=htonl(INADDR_ANY);

    bind(s,(struct sockaddr*)&addr,sizeof(addr));
    return s;
}

void Connect(int s){
    struct sockaddr_in addr;
    socklen_t len;
    while(1){
        ssize_t sz=0;
        char buf[1024]={0};
        sz=recvfrom(s,buf,sizeof(buf),0,(struct sockaddr*)&addr,&len );
        {
            cout<

你可能感兴趣的:(网络编程)