TCP/IP(Transmission Control Protocol/Internet Protocol)即传输控制协议/网间协议,是一个工业标准的协议集,它是为广域网(WANs)设计的。
UDP(User Data Protocol,用户数据报协议)是与TCP相对应的协议。它是属于TCP/IP协议族中的一种。
协议的关系图
TCP/IP协议族包括运输层、网络层、链路层。现在你知道TCP/IP与UDP的关系了吧。
Socket是什么呢?
Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口。在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐藏在Socket接口后面,对用户来说,一组简单的接口就是全部,让Socket去组织数据,以符合指定的协议。
先从服务器端说起。服务器端先初始化Socket,然后与端口绑定(bind),对端口进行监听(listen),调用accept阻塞,等待客户端连接。在这时如果有个客户端初始化一个Socket,然后连接服务器(connect),如果连接成功,这时客户端与服务器端的连接就建立了。客户端发送数据请求,服务器端接收请求并处理请求,然后把回应数据发送给客户端,客户端读取数据,最后关闭连接,一次交互结束。
服务器端的代码为
- #include <stdio.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <stdlib.h>
- #include <syslog.h>
- #include <errno.h>
- #define MAX_LISTEN_NUM 5
- #define SEND_BUF_SIZE 100
- #define RECV_BUF_SIZE 100
- #define LISTEN_PORT 1010
- int main()
- {
- int listen_sock = 0;
- int app_sock = 0;
- struct sockaddr_in hostaddr;
- struct sockaddr_in clientaddr;
- int socklen = sizeof(clientaddr);
- char sendbuf[SEND_BUF_SIZE] = {0};
- char recvbuf[RECV_BUF_SIZE] = {0};
- int sendlen = 0;
- int recvlen = 0;
- int retlen = 0;
- int leftlen = 0;
- char *ptr = NULL;
- memset((void *)&hostaddr, 0, sizeof(hostaddr));
- memset((void *)&clientaddr, 0, sizeof(clientaddr));
- hostaddr.sin_family = AF_INET;
- hostaddr.sin_port = htons(LISTEN_PORT);
- hostaddr.sin_addr.s_addr = htonl(INADDR_ANY);
- listen_sock = socket(AF_INET, SOCK_STREAM, 0);
- if(listen_sock < 0)
- {
- syslog(LOG_ERR, "%s:%d, create socket failed", __FILE__, __LINE__);
- exit(1);
- }
- if(bind(listen_sock, (struct sockaddr *)&hostaddr, sizeof(hostaddr)) < 0)
- {
- syslog(LOG_ERR, "%s:%d, bind socket failed", __FILE__, __LINE__);
- exit(1);
- }
- if(listen(listen_sock, MAX_LISTEN_NUM) < 0)
- {
- syslog(LOG_ERR, "%s:%d, listen failed", __FILE__, __LINE__);
- exit(1);
- }
- while(1)
- {
- app_sock = accept(listen_sock, (struct sockaddr *)&clientaddr, &socklen);
- if(app_sock < 0)
- {
- syslog(LOG_ERR, "%s:%d, accept failed", __FILE__, __LINE__);
- exit(1);
- }
- sprintf(sendbuf, "welcome %s:%d here!/n", inet_ntoa(clientaddr.sin_addr.s_addr), clientaddr.sin_port);
- //send data
- sendlen = strlen(sendbuf) +1;
- retlen = 0;
- leftlen = sendlen;
- ptr = sendbuf;
- //while(leftlen)
- {
- retlen = send(app_sock, ptr, sendlen, 0);
- if(retlen < 0)
- {
- if(errno == EINTR)
- retlen = 0;
- else
- exit(1);
- }
- leftlen -= retlen;
- ptr += retlen;
- }
- //receive data
- recvlen = 0;
- retlen = 0;
- ptr = recvbuf;
- leftlen = RECV_BUF_SIZE -1;
- //do
- {
- retlen = recv(app_sock, ptr, leftlen, 0) ;
- if(retlen < 0)
- {
- if(errno == EINTR)
- retlen = 0;
- else
- exit(1);
- }
- recvlen += retlen;
- leftlen -= retlen;
- ptr += retlen;
- }
- //while(recvlen && leftlen);
- printf("receive data is : %s", recvbuf);
- close(app_sock);
- }
- close(listen_sock);
- return 0;
- }
客户端代码为:
- #include <stdio.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <syslog.h>
- #include <errno.h>
- #include <stdlib.h>
- #define MAX_LISTEN_NUM 5
- #define SEND_BUF_SIZE 100
- #define RECV_BUF_SIZE 100
- #define SERVER_PORT 1010
- int main()
- {
- int sock_fd = 0;
- char recvbuf[RECV_BUF_SIZE] = {0};
- char sendbuf[SEND_BUF_SIZE] = {0};
- int recvlen = 0;
- int retlen = 0;
- int sendlen = 0;
- int leftlen = 0;
- char *ptr = NULL;
- struct sockaddr_in ser_addr;
- memset(&ser_addr, 0, sizeof(ser_addr));
- ser_addr.sin_family = AF_INET;
- inet_aton("127.0.0.1", (struct in_addr *)&ser_addr.sin_addr);
- ser_addr.sin_port = htons(SERVER_PORT);
- sock_fd = socket(AF_INET, SOCK_STREAM, 0);
- if(sock_fd < 0)
- {
- syslog(LOG_ERR, "%s:%d, create socket failed", __FILE__, __LINE__);
- exit(1);
- }
- if(connect(sock_fd, (struct sockaddr *)&ser_addr, sizeof(ser_addr)) < 0)
- {
- syslog(LOG_ERR, "%s:%d, connect socket failed", __FILE__, __LINE__);
- exit(1);
- }
- //receive data
- recvlen = 0;
- retlen = 0;
- ptr = recvbuf;
- leftlen = RECV_BUF_SIZE -1;
- //do
- {
- retlen = recv(sock_fd, ptr, leftlen, 0) ;
- if(retlen < 0)
- {
- if(errno == EINTR)
- retlen = 0;
- else
- exit(1);
- }
- recvlen += retlen;
- leftlen -= retlen;
- ptr += retlen;
- }
- //while(recvlen && leftlen);
- printf("receive data is : %s", recvbuf);
- sprintf(sendbuf, "hello server/n");
- //send data
- sendlen = strlen(sendbuf) +1;
- retlen = 0;
- leftlen = sendlen;
- ptr = sendbuf;
- // while(leftlen)
- {
- retlen = send(sock_fd, ptr, sendlen, 0);
- if(retlen < 0)
- {
- if(errno == EINTR)
- retlen = 0;
- else
- exit(1);
- }
- leftlen -= retlen;
- ptr += retlen;
- }
- close(sock_fd);
- }