??这一系列文章的目的是在学习了C++基础后,继续补充一些C++基础和进阶的知识点,包括C++11的相关内容。
1 TCP
client
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
return -1;
struct sockaddr_in srv;
srv.sin_family=AF_INET;
srv.sin_port=htons(atoi("1122"));
srv.sin_addr.s_addr=inet_addr("192.168.1.100");
socklen_t len=sizeof(struct sockaddr_in);
if(connect(sockfd,(struct sockaddr*)&srv,len)<0)
return -1;
unsigned char buff_recv[1024];
memset(buff_recv, 0x00, sizeof(buff_recv));
recv(sockfd, buff_recv, sizeof(buff_recv)-1, 0);
unsigned char buff_send[2]={0x01,0x02};
send(sockfd, buff_send, sizeof(buff_send), 0);
close(sockfd);
server
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
return -1;
struct sockaddr_in server;
server.sin_family=AF_INET;
server.sin_port=htons(atoi("1122"));
server.sin_addr.s_addr=inet_addr("192.168.1.100");
int bind_result = bind(sockfd, (struct sockaddr*)&server, sizeof(server));
if (bind_result == -1)
{
close(sockfd);
return -1;
}
int listen_result = listen(sockfd,5);
if(listen_result == -1)
return -1;
struct sockaddr_in client;
socklen_t len = sizeof(client);
int new_fd = accept(sockfd, (struct sockaddr*)&client, &len);
unsigned char buff_recv[1024];
memset(buff_recv, 0x00, sizeof(buff_recv));
recv(new_fd, buff_recv, sizeof(buff_recv)-1, 0);
unsigned char buff_send[2]={0x01,0x02};
send(new_fd, buff_send, sizeof(buff_send), 0);
close(sockfd);
2 UDP
广播发送
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(-1==sockfd)
return -1;
const int opt = 1;
int bro_result = setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, (char *)&opt, sizeof(opt));
if(bro_result == -1)
return -1;
struct sockaddr_in addr_to;
socklen_t addr_to_len=sizeof(addr_to);
memset(&addr_to, 0, addr_to_len);
addr_to.sin_family=AF_INET;
addr_to.sin_port=htons(6000);
addr_to.sin_addr.s_addr=htonl(INADDR_BROADCAST);
char buffer[] = {"abcdef"};
int ret=sendto(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*)&addr_to, addr_to_len);
close(sockfd);
广播接收
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(-1==sockfd)
return -1;
struct sockaddr_in addr_local;
socklen_t addr_local_len=sizeof(addr_local);
memset(&addr_local, 0, addr_local_len);
addr_local.sin_family = AF_INET;
addr_local.sin_port = htons(6000);
addr_local.sin_addr.s_addr = htonl(INADDR_ANY);
int bind_result = bind(sockfd, (struct sockaddr*)&addr_local, addr_local_len);
if (bind_result == -1)
{
close(sockfd);
return -1;
}
char buffer[30];
memset(buffer, 0, 30);
struct sockaddr_in addr_from;
socklen_t addr_from_len=sizeof(addr_from);
int len = recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*)&addr_from, &addr_from_len);
close(sockfd);
3 非阻塞接收
#include <fcntl.h>
int flag_fcntl = fcntl(sockfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, flag_fcntl|O_NONBLOCK);
4 recv recvfrom read send sendto write
ssize_t write(int fd, const void*buf, size_t nbytes);
ssize_t read(int fd, void *buf, size_t nbyte);
int recv(int sockfd, void *buf, int len, int flags);
int send(int sockfd, void *buf, int len, int flags);
int recvfrom(int sockfd, void *buf, int len, unsigned int flags, struct sockaddr *from, int *fromlen);
int sendto(int sockfd, const void *buf, int len, unsigned int flags, const struct sockaddr *to, int tolen);
- 参考:Linux下,write/read,recv/send, recvfrom/sendto的区别
- read/wirte是通用的文件描述符操作;recv/send 通常应用于TCP;recvfrom/sendto通常应用于UDP。
- recv/send函数的最后一个参数为0时,功能上与read和write基本一致。
- read/wirte:从文件描述符fd中直接读写数据。
- recv/send:增加第四个参数控制读写操作。
- recvfrom/sendto:接收数据时获取对方IP,发送数据时指明对方IP。
|