目录
1 TCP通信基本流程
图 1
2 示例程序
2.1 服务端程序
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define LISTEN_BACKLOG (5)
#define BUF_SIZE (1500)
#define ACK_STR "ack ok"
void usage(void) {
printf("*********************************\n");
printf("./server 本端ip 本端端口\n");
printf("*********************************\n");
}
int main(int argc, char *argv[]) {
struct sockaddr_in client;
struct sockaddr_in server;
int sock_fd = 0, new_fd = 0;
int ret = 0;
socklen_t addrlen = 0;
char send_buf[BUF_SIZE] = {0};
char recv_buf[BUF_SIZE] = {0};
if (argc != 3) {
usage();
return -1;
}
char *ip = argv[1];
unsigned short port = atoi(argv[2]);
printf("ip:port->%s:%u\n", argv[1], port);
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if (sock_fd == -1) {
perror("socket error");
return -1;
}
memset(&client, 0, sizeof(struct sockaddr_in));
client.sin_family = AF_INET;
client.sin_addr.s_addr = inet_addr(ip);
client.sin_port = htons(port);
ret = bind(sock_fd, (struct sockaddr *)&client,
sizeof(struct sockaddr));
if (ret == -1) {
close(sock_fd);
perror("bind error");
return -1;
}
ret = listen(sock_fd, LISTEN_BACKLOG);
if (ret == -1) {
close(sock_fd);
perror("listen error");
return -1;
}
addrlen = sizeof(server);
new_fd = accept(sock_fd, (struct sockaddr *)&server, &addrlen);
if (new_fd == -1) {
close(sock_fd);
perror("accept error");
return -1;
}
int seq = 0;
while(1) {
memset(recv_buf, 0, BUF_SIZE);
ret = recv(new_fd, recv_buf, BUF_SIZE, 0);
printf("recv len:%d, %s\n", ret, recv_buf);
memset(send_buf, 0, BUF_SIZE);
sprintf(send_buf, "%s:%d\n", ACK_STR, seq++);
ret = send(new_fd, send_buf, strlen(send_buf), 0);
printf("send len:%d %s\n", ret, send_buf);
}
close(sock_fd);
close(new_fd);
return 0;
}
2.2 客户端程序
#include <stdio.h>#include <string.h>
#include <unistd.h>#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define LISTEN_BACKLOG (5)
#define BUF_SIZE (1500)
#define REQUEST_STR "tcp pack"
void usage(void) {
printf("*********************************\n");
printf("./client 对端ip 对端端口\n");
printf("*********************************\n");
}
int main(int argc, char *argv[]) {
struct sockaddr_in client;
struct sockaddr_in server;
int sock_fd = 0;
int ret = 0;
socklen_t addrlen = 0;
char send_buf[BUF_SIZE] = {0};
char recv_buf[BUF_SIZE] = {0};
if (argc != 3) {
usage();
return -1;
}
char *ip = argv[1];
unsigned short port = atoi(argv[2]);
printf("ip:port->%s:%u\n", argv[1], port);
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if (sock_fd == -1) {
perror("socket error");
return -1;
}
memset(&server, 0, sizeof(struct sockaddr_in));
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(ip);
server.sin_port = htons(port);
ret = connect(sock_fd, (struct sockaddr *)&server,
sizeof(struct sockaddr));
if (ret == -1) {
close(sock_fd);
perror("connect error");
return -1;
}
int seq = 0;
while(1) {
sleep(1);
memset(send_buf, 0, BUF_SIZE);
sprintf(send_buf, "%s:%d", REQUEST_STR, seq++);
send(sock_fd, send_buf, strlen(send_buf), 0);
printf("send %s\n", send_buf);
memset(recv_buf, 0, BUF_SIZE);
recv(sock_fd, recv_buf, BUF_SIZE, 0);
printf("recv %s\n", recv_buf);
}
close(sock_fd);
return 0;
}
3 调试
3.1 编译
服务端#gcc server.c -o server
客户端#gcc client.c -o client
3.2 运行
服务端命令
#./server 本端ip 本端端口客户端命令
#./client 对端ip 对端端口
调试输出:
服务端打印
客户端打印