
TCP服务端:
TCP客户端:
int socket(int domain,int type,int protocol)
1.domain:指明所使用的协议,通常为AF_INEF,表示互联网协议族(TCP/IP协议族)
2.type:指定socket的类型
3.protocol:通常赋值为0
成功返回socket套接字描述符,失败返回-1
用于绑定IP地址和端口号到socketfd
int bind(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
man手册给的第二个参数是:

但是一般写成如下形式,再强转为struct sockaddr *形式

例如如下写法:

int listen(int sockfd, int backlog);
成功返回0,失败返回-1,并且errno中包含相应的错误码
用于绑定之后的client端,与服务器建立连接
int connect(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
成功返回0,失败返回-1,并且errno中包含相应的错误码
accept函数由TCP服务器调用,用于从已经完成连接队列队头返回下一个已完成连接,如果已完成连接队列为空,那么进程之间进入睡眠。
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
该函数的返回值是一个新的套接字描述符,返回值是表示已连接的套接字描述符,而第一个参数是服务器监听套接字描述都,一个服务器通常仅仅创建一个监听套接字,它在服务器的生命周期内一直存在。内核为每个由服务器进程接受的客户端连接创建一个已连接套接字(表示TCP三次握手协议已完成),当服务器完成对某个给定客户的服务时,响应的已连接套接字就会被关闭。
与文件编程中使用的read、write是一模一样的,不过多赘述
此外还要另外两种数据收发API:
其实和read、write也是差不多的,只不过多了一个flag参数,flag表示控制选项,一般设置为0。
#include
uint32_t htonl(uint32_t hostlong);返回网络字节序的值
uint16_t htons(uint16_t hostshort);返回网络字节序的值
uint32_t ntohl(uint32_t netlong);返回主机字节序的值
uint16_t ntohs(uint16_t netshort);返回主机字节序的值uint32_t
h代表host,n代表net,s代表short(两个字节),l代表long(四个字节),通过上面4个函数可以表示主机字节序和网络字节序之间的转换。有时可以用INADDR_ANY , INADDR_ANY指定地址让操作系统自己获取。
一般用下面黑体标出的两个
#include
#include
#include
int inet_aton(const char *cp, struct in_addr *inp);把字符串形式的192.168.1.123转为网络能识别的格式
in_addr_t inet_addr(const char *cp);
in_addr_t inet_network(const char *cp);
char *inet_ntoa(struct in_addr in);把网络格式的ip地址转为字符串形式
struct in_addr inet_makeaddr(int net, int host);
in_addr_t inet_lnaof(struct in_addr in);
in_addr_t inet_netof(struct in_addr in);
服务端:连接客户端并读取客户端IP、发送的信息并返回读取状态。
- #include
- #include
- #include
- //#include
- #include
- #include
- #include
- #include
-
- int main()
- {
- int s_fd;
- char readBuf[128];
- memset(readBuf,0,sizeof(readBuf));
- int nread=0;
- struct sockaddr_in s_addr;
- struct sockaddr_in c_addr;
-
- memset(&s_addr,0,sizeof(struct aockaddr_in *));
- memset(&s_addr,0,sizeof(struct aockaddr_in *));
-
- s_fd=socket(AF_INET,SOCK_STREAM,0);
-
- if(s_fd==-1)
- {
- printf("creat soclet failed\n");
- perror("socket");
- exit(-1);
- }
-
- s_addr.sin_family=AF_INET;
- s_addr.sin_port=htons(9090);
- inet_aton("169.254.6.127",&(s_addr.sin_addr));
-
- bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
-
- listen(s_fd,10);
-
- int clen=sizeof(struct sockaddr_in);
- int c_fd=accept(s_fd,(struct sockaddr *)&c_addr,&clen);
-
- if(c_fd == -1)
- {
- perror("acccept");
- }
-
- printf("get connect :%s\n",inet_ntoa(c_addr.sin_addr));
-
- nread=read(c_fd,readBuf,128);
- if(nread ==-1)
- {
- perror("read");
- }
- else if(nread>0)
- {
- printf("get message:%d,%s\n",nread,readBuf);
- }
- else
- {
- printf("client quit\n");
- }
-
- write(c_fd,"hhhhhhhhhhhhhhh",128);
-
-
- return 0;
- }

客户端:
- #include
- #include
- #include
- //#include
- #include
- #include
- #include
- #include
- int main()
- {
-
- int c_fd;
- char readBuf[128];
- memset(readBuf,0,sizeof(readBuf));
- int nread=0;
- struct sockaddr_in c_addr;
-
- memset(&c_addr,0,sizeof(struct aockaddr_in *));
-
- c_fd=socket(AF_INET,SOCK_STREAM,0);
-
- if(c_fd==-1)
- {
- printf("creat soclet failed\n");
- perror("socket");
- exit(-1);
- }
- c_addr.sin_family=AF_INET;
- c_addr.sin_port=htons(9090);
- inet_aton("169.254.6.127",&(c_addr.sin_addr));
-
- if(connect(c_fd,(struct sockaddr *)&c_addr,sizeof(struct sockaddr)) ==-1)
-
- {
- perror("connect");
- exit(-1);
- }
-
-
- write(c_fd,"xxxxxxxxx",128);
-
- nread=read(c_fd,readBuf,128);
- if(nread ==-1)
- {
- perror("read");
- }
- else
- {
- printf("get message from sever:%d,%s\n",nread,readBuf);
- }
-
-
- return 0;
- }
