博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BSD Socket~TCP~Example Code
阅读量:7087 次
发布时间:2019-06-28

本文共 4109 字,大约阅读时间需要 13 分钟。

TCP 协议实现 C版本号,可用于Mac OS X机器上执行

Server: 

/*Setting up a simple TCP server involves the following steps:Creating a TCP socket, with a call to socket().Binding the socket to the listen port, with a call to bind(). Before calling bind(), a programmer must declare a sockaddr_in structure, clear it (with memset()), and the sin_family (AF_INET), and fill its sin_port (the listening port, in network byte order) fields. Converting a short int to network byte order can be done by calling the function htons() (host to network short).Preparing the socket to listen for connections (making it a listening socket), with a call to listen().Accepting incoming connections, via a call to accept(). This blocks until an incoming connection is received, and then returns a socket descriptor for the accepted connection. The initial descriptor remains a listening descriptor, and accept() can be called again at any time with this socket, until it is closed.Communicating with the remote host, which can be done through send() and recv() or write() and read().Eventually closing each socket that was opened, once it is no longer needed, using close().Code may set up a TCP server on port 1100 as follows:*//* Server code in C */   #include 
#include
#include
#include
#include
#include
#include
#include
int main(void) { struct sockaddr_in stSockAddr; int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if(-1 == SocketFD) { perror("can not create socket"); exit(EXIT_FAILURE); } memset(&stSockAddr, 0, sizeof(stSockAddr)); stSockAddr.sin_family = AF_INET; stSockAddr.sin_port = htons(1100); stSockAddr.sin_addr.s_addr = htonl(INADDR_ANY); if(-1 == bind(SocketFD,(struct sockaddr *)&stSockAddr, sizeof(stSockAddr))) { perror("error bind failed"); close(SocketFD); exit(EXIT_FAILURE); } if(-1 == listen(SocketFD, 10)) { perror("error listen failed"); close(SocketFD); exit(EXIT_FAILURE); } for(;;) { int ConnectFD = accept(SocketFD, NULL, NULL); if(0 > ConnectFD) { perror("error accept failed"); close(SocketFD); exit(EXIT_FAILURE); } /* perform read write operations ... read(ConnectFD,buff,size)*/ if (-1 == shutdown(ConnectFD, SHUT_RDWR)) { perror("can not shutdown socket"); close(ConnectFD); close(SocketFD); exit(EXIT_FAILURE); } close(ConnectFD); } close(SocketFD); return EXIT_SUCCESS; }

Client

/*Programming a TCP client application involves the following steps:Creating a TCP socket, with a call to socket().Connecting to the server with the use of connect(), passing a sockaddr_in structure with the sin_family set to AF_INET, sin_port set to the port the endpoint is listening (in network byte order), and sin_addr set to the IP address of the listening server (also in network byte order.)Communicating with the server by using send() and recv() or write() and read().Terminating the connection and cleaning up with a call to close().*/  /* Client code in C */   #include 
#include
#include
#include
#include
#include
#include
#include
int main(void) { struct sockaddr_in stSockAddr; int Res; int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (-1 == SocketFD) { perror("cannot create socket"); exit(EXIT_FAILURE); } memset(&stSockAddr, 0, sizeof(stSockAddr)); stSockAddr.sin_family = AF_INET; stSockAddr.sin_port = htons(1100); Res = inet_pton(AF_INET, "192.168.1.3", &stSockAddr.sin_addr); if (0 > Res) { perror("error: first parameter is not a valid address family"); close(SocketFD); exit(EXIT_FAILURE); } else if (0 == Res) { perror("char string (second parameter does not contain valid ipaddress)"); close(SocketFD); exit(EXIT_FAILURE); } if (-1 == connect(SocketFD, (struct sockaddr *)&stSockAddr, sizeof(stSockAddr))) { perror("connect failed"); close(SocketFD); exit(EXIT_FAILURE); } /* perform read write operations ... */ (void) shutdown(SocketFD, SHUT_RDWR); close(SocketFD); return EXIT_SUCCESS; }
Link:

BSD Socket  Wikipedia

转载地址:http://gdgml.baihongyu.com/

你可能感兴趣的文章
linux内核编译安装
查看>>
在CentOS/RHEL/Scientific Linux 6 & 7 上安装Telnet
查看>>
linux中hash命令:显示、添加或清除哈希表
查看>>
理解MySQL复制(Replication)——经典文献
查看>>
安卓手机recovery下刷补丁提示:“can't open /sdcard/update.zip(bad)”
查看>>
shared_ptr源码分析
查看>>
AOJ 2164 Revenge of the Round Table 题解《挑战程序设计竞赛》
查看>>
What's new in JSF 2.2
查看>>
eclipse 一直buildingWorkSpace 卡死解决
查看>>
Hyper-V导入Ubuntu虚拟机后发现网卡eth0丢失的解决办法
查看>>
Web server和php结合的三种模式
查看>>
Linux中的LDAP认证
查看>>
数组竟然可以这样定义
查看>>
Hyperledger Fabric 链码(智能合约)基本操作
查看>>
再学 GDI+[77]: 区域(6) - GetRegionScans - 获取区域中的所有矩形
查看>>
学习 TList 类的实现[7]
查看>>
配置Hyper-V Server 资源计量
查看>>
创建 GUID
查看>>
String
查看>>
Linux命令总结1
查看>>