生活随笔
收集整理的這篇文章主要介紹了
网络编程套接字(二)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
網(wǎng)絡(luò)編程套接字(二)
文章目錄
- 網(wǎng)絡(luò)編程套接字(二)
- 一、簡(jiǎn)單的UDP網(wǎng)絡(luò)程序
一、簡(jiǎn)單的UDP網(wǎng)絡(luò)程序
#pragma once
#include <cstdio>
#include <cstring>
#include <string>
#include <cassert>
#include <cstdlib>#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>typedef struct sockaddr sockaddr
;
typedef struct sockaddr_in sockaddr_in
;class UdpSocket
{public:UdpSocket():fd_(-1){}bool Socket(){fd_
= socket(AF_INET
,SOCK_DGRAM
,0);if(fd_
< 0){perror("socket error");return false;}return true;}bool Close(){close(fd_
);return true;}bool Bind(const std
::string
& ip
,uint16_t port
){sockaddr_in addr
;addr
.sin_family
= AF_INET
;addr
.sin_addr
.s_addr
= inet_addr(ip
.c_str());addr
.sin_port
= htons(port
);int ret
= bind(fd_
,(sockaddr
*)&addr
,sizeof(addr
));if(ret
< 0){perror("bind is error");return false;}return true;}bool RecvFrom(std
::string
* buf
,std
::string
* ip
= nullptr,uint16_t* port
= nullptr){char temp
[1024 * 10] = {0};sockaddr_in peer
;socklen_t len
= sizeof(peer
);ssize_t read_size
= recvfrom(fd_
,temp
,sizeof(temp
) -1,0,(sockaddr
*)&peer
,&len
);if(read_size
< 0){perror("recvfrom is error");return false;}buf
->assign(temp
,read_size
);if(ip
!= nullptr){*ip
= inet_ntoa(peer
.sin_addr
);}if(port
!= nullptr){*port
= ntohs(peer
.sin_port
);}return true;}bool SendTo(const std
::string
& buf
,const std
::string
& ip
,uint16_t port
){sockaddr_in addr
;addr
.sin_family
= AF_INET
;addr
.sin_addr
.s_addr
= inet_addr(ip
.c_str());addr
.sin_port
= htons(port
);ssize_t write_size
= sendto(fd_
,buf
.data(),buf
.size(),0,(sockaddr
*)&addr
,sizeof(addr
));if(write_size
< 0){perror("sendto is error");return false;}return true;}private:int fd_
;
};
#pragma once
#include <functional>
#include "udp_socket.hpp"typedef std
::function
<void(const std
::string
&, std
::string
* resp
)> Handler
;class UdpServer
{public:UdpServer(){assert(sock_
.Socket());}~UdpServer(){sock_
.Close();}bool Start(const std
::string
& ip
, uint16_t port
,Handler handler
){printf("server is start!\n");bool ret
= sock_
.Bind(ip
,port
);if(!ret
){return false;}while(1){std
::string req
;std
::string remote_ip
;uint16_t remote_port
;bool ret
= sock_
.RecvFrom(&req
,&remote_ip
,&remote_port
);if(!ret
){continue;}std
::string resp
;handler(req
,&resp
);ret
= sock_
.SendTo(resp
,remote_ip
,remote_port
);printf("[%s : %d] req: %s, resp: %s\n",remote_ip
.c_str(),remote_port
,req
.c_str(),resp
.c_str());}sock_
.Close();return true;}private:UdpSocket sock_
;
};
#pragma once
#include "udp_socket.hpp"class UdpClient
{public:UdpClient(const std
::string
& ip
,uint16_t port
):ip_(ip
),port_(port
){assert(sock_
.Socket());}~UdpClient(){sock_
.Close();}bool RecvFrom(std
::string
* buf
){return sock_
.RecvFrom(buf
);}bool Sendto(const std
::string
& buf
){return sock_
.SendTo(buf
,ip_
,port_
);}private:UdpSocket sock_
;std
::string ip_
;uint16_t port_
;
};
#include "udp_server.hpp"
#include <iostream>
#include <unordered_map>std
::unordered_map
<std
::string
,std
::string
> g_dict
;void Translate(const std
::string
& req
,std
::string
* resp
)
{auto it
= g_dict
.find(req
);if(it
== g_dict
.end()){*resp
= "未查到";return;}*resp
= it
->second
;
}#if 0
int main(int argc
,char* argv
[])
{if(argc
!= 3){printf("./ ip ,port\n");return 1;}g_dict
.insert(std
::make_pair("hello", "你好"));g_dict
.insert(std
::make_pair("world", "世界"));g_dict
.insert(std
::make_pair("c++", "最好的編程語言"));g_dict
.insert(std
::make_pair("bit", "特別NB"));UdpServer server
;server
.Start(argv
[1],atoi(argv
[2]),Translate
);return 0;
}
#endifint main()
{g_dict
.insert(std
::make_pair("world", "世界"));g_dict
.insert(std
::make_pair("c++", "最好的編程語言"));g_dict
.insert(std
::make_pair("bit", "特別NB"));g_dict
.insert(std
::make_pair("hello", "您好"));UdpServer server
;server
.Start("0.0.0.0",9090,Translate
);return 0;
}
#include "udp_client.hpp"
#include <iostream>int main(int argc
, char* argv
[])
{if (argc
!= 3) {printf("Usage ./dict_client [ip] [port]\n");return 1;}UdpClient
client(argv
[1], atoi(argv
[2]));for (;;){std
::string word
;std
::cout
<< "請(qǐng)輸入您要查的單詞: ";std
::cin
>> word
;client
.Sendto(word
);std
::string result
;client
.RecvFrom(&result
);std
::cout
<< word
<< " 意思是 " << result
<< std
::endl
;}return 0;
}
總結(jié)
以上是生活随笔為你收集整理的网络编程套接字(二)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。