生活随笔
收集整理的這篇文章主要介紹了
TCP编程模型
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
1 TCP編程模型
先來看一下socket整體框架圖:
如下為TCP通信模型:
TCP客戶端和服務器示例代碼:
demo_tcp_server.c:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>#include <netinet/in.h>
#include <pthread.h>
#include <malloc.h>
#include <string.h>#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>#include <netdb.h>#include <sys/time.h>#include <signal.h>#define SERVER_PORT_TCP 6666
#define TCP_BACKLOG 10
int sock_fd
, new_fd
;void printf_hex(char *buf
, int len
)
{int i
;for(i
= 0; i
< len
; i
++){printf("0x%x ", buf
[i
]);}}void sig_chld(int signo
)
{pid_t pid
;int stat
;while((pid
= waitpid(-1, &stat
, WNOHANG
)) > 0)printf("child %d terminated\r\n", pid
);return;
}int main(void)
{char command
[1024];char *str
;struct sockaddr_in my_addr
;struct sockaddr_in their_addr
;int sin_size
;struct sockaddr_in
*cli_addr
;if((sock_fd
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1){perror("socket is error\r\n");exit(1);}my_addr
.sin_family
= AF_INET
;my_addr
.sin_port
= htons(6666);my_addr
.sin_addr
.s_addr
= INADDR_ANY
;bzero(&(my_addr
.sin_zero
), 8);if(bind(sock_fd
, (struct sockaddr
*)&my_addr
, sizeof(struct sockaddr
)) == -1){perror("bind is error\r\n");exit(1);}if(listen(sock_fd
, TCP_BACKLOG
) == -1){perror("listen is error\r\n");exit(1);}printf("start accept\n");signal(SIGCHLD
, sig_chld
); while(1){sin_size
= sizeof(struct sockaddr_in
);if((new_fd
= accept(sock_fd
, (struct sockaddr
*)&their_addr
, (socklen_t
*)&sin_size
)) == -1){perror("accept");continue;}cli_addr
= malloc(sizeof(struct sockaddr
));printf("accept addr\r\n");if(cli_addr
!= NULL){memcpy(cli_addr
, &their_addr
, sizeof(struct sockaddr
));}ssize_t ret
;char recvbuf
[512];char *buf
= "hello! I'm server!";while(1){if((ret
= recv(new_fd
, recvbuf
, sizeof(recvbuf
), 0)) == -1){printf("recv error \r\n");return -1;}printf("recv :\r\n");printf("%s", recvbuf
);printf("\r\n");sleep(2);if((ret
= send(new_fd
, buf
, strlen(buf
) + 1, 0)) == -1){perror("send : ");}sleep(2);}close(new_fd
);}return 0;
}
demo_tcp_client.c:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#include <malloc.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>#include <netdb.h>#include <sys/time.h>#include <signal.h>int sockfd
;
#define SERVER_IP "127.0.0.1"#define SERVER_PORT 6666void printf_hex(char *buf
, int len
)
{int i
;for(i
= 0; i
< len
; i
++){printf("0x%x ", buf
[i
]);}}int main(void)
{char command
[1024];char *str
;struct sockaddr_in their_addr
; if ((sockfd
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1){perror("socket");
}their_addr
.sin_family
= AF_INET
;their_addr
.sin_port
= htons(SERVER_PORT
);their_addr
.sin_addr
.s_addr
= inet_addr(SERVER_IP
);bzero(&(their_addr
.sin_zero
), 8);if(connect(sockfd
, (struct sockaddr
*)&their_addr
, sizeof(struct sockaddr
)) == -1){perror("connect");exit(1);}ssize_t ret
;char recvbuf
[512];char *buf
= "hello! I'm client!";while(1){if((ret
= send(sockfd
, buf
, strlen(buf
) + 1, 0)) == -1){perror("send : ");}sleep(2);if((ret
= recv(sockfd
, &recvbuf
, sizeof(recvbuf
), 0)) == -1){return -1;}printf("recv :\r\n");printf("%s", recvbuf
);printf("\r\n");sleep(2);}close(sockfd
);return 0;
}
參考資料:
5G物聯網云平臺智能家居項目30天搞定
總結
以上是生活随笔為你收集整理的TCP编程模型的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。