编写一个最为简单的tcp通信服务端。代码如下:
#include <iostream>
#include <cstring>
using namespace std;
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <unistd.h>
int main()
{
/*创建socket*/
int sd = socket(AF_INET, SOCK_STREAM, 0);
if (sd == -1)
{
cout << "socket creat error" << endl;
cout << errno << ":" << strerror(errno) << endl;exit(-1);
}
/*定义地址*/
struct sockaddr_in addr = {0};
addr.sin_family = AF_INET;
addr.sin_port = htons(12222);
inet_aton("192.168.56.11", &addr.sin_addr);
/*绑定地址*/
int bd_ret = bind(sd, (struct sockaddr*)&addr, sizeof(addr));
if (bd_ret != 0)
{
cout << "socket