unixsocket网络编程TCP简单例子-创新互联
成都创新互联是一家专注于成都网站制作、网站建设与策划设计,连江网站建设哪家好?成都创新互联做网站,专注于网站建设十载,网设计领域的专业建站公司;建站业务涵盖:连江等地区。连江做网站价格咨询:18980820575
网站栏目:unixsocket网络编程TCP简单例子-创新互联
标题URL:http://azwzsj.com/article/dhjegj.html
- server.c
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- int main(int argc, char** argv)
- {
- int ret;
- //创建socket文件
- int sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if(sockfd == -1)
- {
- perror("socket\n");
- exit(1);
- }
- printf("socket_fd = %d\n",sockfd);
- //重复使用本地址与socket文件进行绑定,如果不设置系统,会保留此连接直到最后一引用才释放,即进程结束后系统需要几分钟后才能重新进行绑定
- int i = 1;
- ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
- if(ret)
- {
- perror("setsockopt\n");
- exit(1);
- }
- printf("setsockopt complete!\n");
- //创建服务器socket地址sock_addr
- struct sockaddr_in server_addr;
- server_addr.sin_family = AF_INET;
- server_addr.sin_port = htons(4444);
- //地址指定INADDR_ANY表示可以接收安装在系统里面所有网卡的数据包
- server_addr.sin_addr.s_addr = INADDR_ANY;
- memset(server_addr.sin_zero, 8, 8);
- //将socket与地址进行绑定,这样可以使所有发向端口4444及协议为IPV4数据包指向socket文件
- ret = bind(sockfd, (struct sockaddr*)&server_addr, sizeof(struct sockaddr));
- if(ret)
- {
- perror("bind\n");
- exit(1);
- }
- printf("bind complete!\n");
- //监听连接到该socket上请求,不会发生阻塞行为,以下设置队列大小为1,即队列满的时候其它连接会被拒绝连接
- ret = listen(sockfd, 10);
- if(ret)
- {
- perror("listen\n");
- exit(1);
- }
- printf("listen complete!\n");
- //接收连接请求并建立连接, 如果没有连接请求 accept会一址阻塞,直到有请求来
- struct sockaddr_in client_addr;
- int client_fd;
- socklen_t len = sizeof(struct sockaddr);
- client_fd = accept(sockfd, (struct sockaddr*)&client_addr, &len);
- if(client_fd == -1)
- {
- perror("bind\n");
- exit(1);
- }
- printf("accept complete!\n");
- //请取client发来的信息
- char buf[100];
- ssize_t readnum;
- memset(buf,100,0);
- //这里应该是客户端的socket文件描述符
- readnum = recv(client_fd, (void*)buf, 100, MSG_WAITALL);
- //清空标准输出
- fflush(stdout);
- printf("received:%s\nnumber byte:%d\n",buf,strlen(buf));
- //发送信息到用户端
- ssize_t sendnum;
- char content[100]="\"server:received the data!\"";
- sendsendnum = send(client_fd, content, 100, 0);
- printf("reply:%s\nnumber byte:%d\n",content,sendnum);
- // 关闭socket文件
- close(sockfd);
- close(client_fd);
- return 0;
- }
- client.c
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- int main(int argc, char** argv)
- {
- int ret;
- //创建socket文件
- int sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if(sockfd == -1)
- {
- perror("socket\n");
- exit(1);
- }
- printf("socket_fd = %d\n",sockfd);
- //创建服务器socket地址sock_addr
- struct sockaddr_in server_addr;
- server_addr.sin_family = AF_INET;
- server_addr.sin_port = htons(4444);
- //将字符串地址转换成网络地址,有两种方法
- inet_pton(AF_INET, "127.0.0.1", (void*)&server_addr.sin_addr.s_addr);//IPV4 IPV6可用
- //server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");//只能转IPV4
- memset(server_addr.sin_zero, 8, 0);
- //连接服务端
- ret = connect(sockfd, (struct sockaddr*)&server_addr,sizeof(struct sockaddr));
- if(ret == -1)
- {
- perror("connect\n");
- exit(1);
- }
- printf("connect complete!\n");
- //发送信息
- char content[100] = "\"hello,i am client!\"";
- ssize_t sendsendnum = send(sockfd, content, strlen(content)+1, 0);
- printf("send complete\nnumber byte:%d\ncontent:%s\n",sendnum,content);
- //收信息
- char buf[100];
- ssize_t readnum;
- memset(buf,100,0);
- readnum = recv(sockfd, (void*)buf, 100, MSG_WAITALL);
- //清空标准输出
- fflush(stdout);
- printf("received:%s\nnumber byte:%d\n",buf,strlen(buf));
- //关闭socket
- close(sockfd);
- return 0;
- }
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网站栏目:unixsocket网络编程TCP简单例子-创新互联
标题URL:http://azwzsj.com/article/dhjegj.html