- 列表内容
IDE: Clion 2017.01
OS:Ubuntu 16.04 xenial
Main reference: http://blog.chinaunix.net/uid-27007072-id-3390521.html
先打开server.cpp, 连接client收到对方的消息,点到点连接,一对一。
server.cpp 共7步,开头结尾为建立与关闭socket
***
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/time.h>
using namespace std;
#define BUFLEN 1024 //缓冲区长度
#define PORT 6666
#define LISTNUM 20 //等待连接队列的最大长度
int main()
{
int sockfd, newfd;
struct sockaddr_in s_addr, c_addr;
char buf[BUFLEN];
socklen_t len;
unsigned int port, listnum;
fd_set rfds; //Local variable is never used
struct timeval tv;
int retval,maxfd; //Local variable is never used