Skip to content

Commit 553babf

Browse files
Initial commit
1 parent 0914fdf commit 553babf

File tree

11 files changed

+318
-0
lines changed

11 files changed

+318
-0
lines changed

.vscode/settings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"files.associations": {
3+
"stdbool.h": "c",
4+
"unistd.h": "c"
5+
},
6+
"C_Cpp.errorSquiggles": "disabled"
7+
}

client

16 KB
Binary file not shown.

client.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <arpa/inet.h>
2+
#include <netinet/in.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
#include <sys/socket.h>
7+
#include <unistd.h>
8+
9+
#define PORT 8080
10+
11+
int main(int argc, char const *argv[]) {
12+
int sock;
13+
struct sockaddr_in server_addr;
14+
15+
if (argc != 2) {
16+
printf("Usage: %s <server_ip>\\n", argv[0]);
17+
return 1;
18+
}
19+
20+
// Create socket
21+
sock = socket(AF_INET, SOCK_STREAM, 0);
22+
if (sock < 0) {
23+
perror("Socket creation failed");
24+
exit(EXIT_FAILURE);
25+
}
26+
27+
// Set up server address and port number
28+
server_addr.sin_family = AF_INET;
29+
server_addr.sin_port = htons(PORT);
30+
31+
if (inet_pton(AF_INET, argv[1], &server_addr.sin_addr) <= 0) {
32+
fprintf(stderr, "Invalid address\\n");
33+
return 1;
34+
}
35+
36+
// Connect to server
37+
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
38+
perror("Connection failed");
39+
exit(EXIT_FAILURE);
40+
}
41+
42+
printf("Connected to %s:%d\\n", argv[1], PORT);
43+
44+
close(sock);
45+
46+
return 0;
47+
}

client1

16.1 KB
Binary file not shown.

client_print.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <unistd.h>
5+
#include <arpa/inet.h>
6+
7+
#define PORT 8080
8+
9+
int main() {
10+
int sock = 0;
11+
struct sockaddr_in serv_addr;
12+
char *syn_msg = "SYN";
13+
char buffer[1024] = {0};
14+
char *ack_msg = "SYN-ACK";
15+
char *final_ack = "ACK";
16+
17+
// 创建socket文件描述符
18+
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
19+
printf("Socket creation error\n");
20+
return -1;
21+
}
22+
23+
serv_addr.sin_family = AF_INET;
24+
serv_addr.sin_port = htons(PORT);
25+
26+
// 将IP地址转换为二进制形式
27+
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
28+
printf("Invalid address/ Address not supported \n");
29+
return -1;
30+
}
31+
32+
// 连接服务器
33+
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
34+
printf("Connection Failed \n");
35+
return -1;
36+
}
37+
38+
// 第一次握手:发送SYN
39+
send(sock, syn_msg, strlen(syn_msg), 0);
40+
printf("Client sent: %s\n", syn_msg);
41+
42+
// 第二次握手:接收SYN-ACK
43+
read(sock, buffer, 1024);
44+
printf("Client received: %s\n", buffer);
45+
46+
if (strcmp(buffer, ack_msg) == 0) {
47+
// 第三次握手:发送ACK
48+
send(sock, final_ack, strlen(final_ack), 0);
49+
printf("Client sent: %s\n", final_ack);
50+
} else {
51+
printf("Handshake failed.\n");
52+
}
53+
54+
close(sock);
55+
return 0;
56+
}

ip

15.9 KB
Binary file not shown.

ip_protocol.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <stdio.h>
2+
#include <stdint.h>
3+
#include <string.h>
4+
#include <arpa/inet.h> // 添加这个头文件以使用 htons, htonl, ntohs 函数
5+
6+
7+
// IP 头部结构
8+
struct ip_header {
9+
uint8_t version_and_length;
10+
uint8_t type_of_service;
11+
uint16_t total_length;
12+
uint16_t identification;
13+
uint16_t flags_and_fragment_offset;
14+
uint8_t ttl;
15+
uint8_t protocol;
16+
uint16_t checksum;
17+
uint32_t source_ip;
18+
uint32_t dest_ip;
19+
};
20+
21+
// 简单的校验和计算函数
22+
uint16_t calculate_checksum(uint16_t *header, int size) {
23+
uint32_t sum = 0;
24+
for (int i = 0; i < size / 2; i++) {
25+
sum += header[i];
26+
if (sum >> 16) {
27+
sum = (sum & 0xFFFF) + (sum >> 16);
28+
}
29+
}
30+
return ~sum;
31+
}
32+
33+
// 创建 IP 数据包
34+
void create_ip_packet(struct ip_header *ip_packet, uint32_t src_ip, uint32_t dest_ip) {
35+
ip_packet->version_and_length = (4 << 4) | 5; // 版本号 (IPv4) 和 头部长度 (5 * 32-bit words)
36+
ip_packet->type_of_service = 0;
37+
ip_packet->total_length = htons(sizeof(struct ip_header));
38+
ip_packet->identification = htons(54321);
39+
ip_packet->flags_and_fragment_offset = 0;
40+
ip_packet->ttl = 64;
41+
ip_packet->protocol = 6; // TCP
42+
ip_packet->checksum = 0;
43+
ip_packet->source_ip = htonl(src_ip);
44+
ip_packet->dest_ip = htonl(dest_ip);
45+
46+
// 计算校验和
47+
ip_packet->checksum = calculate_checksum((uint16_t *)ip_packet, sizeof(struct ip_header));
48+
}
49+
50+
// 解析 IP 数据包
51+
void parse_ip_packet(struct ip_header *ip_packet) {
52+
printf("IP Version: %d\n", ip_packet->version_and_length >> 4);
53+
printf("Header Length: %d\n", (ip_packet->version_and_length & 0x0F) * 4);
54+
printf("Type of Service: %d\n", ip_packet->type_of_service);
55+
printf("Total Length: %d\n", ntohs(ip_packet->total_length));
56+
printf("Identification: %d\n", ntohs(ip_packet->identification));
57+
printf("TTL: %d\n", ip_packet->ttl);
58+
printf("Protocol: %d\n", ip_packet->protocol);
59+
printf("Checksum: 0x%x\n", ntohs(ip_packet->checksum));
60+
printf("Source IP: %d.%d.%d.%d\n",
61+
ip_packet->source_ip >> 24 & 0xFF,
62+
ip_packet->source_ip >> 16 & 0xFF,
63+
ip_packet->source_ip >> 8 & 0xFF,
64+
ip_packet->source_ip & 0xFF);
65+
printf("Destination IP: %d.%d.%d.%d\n",
66+
ip_packet->dest_ip >> 24 & 0xFF,
67+
ip_packet->dest_ip >> 16 & 0xFF,
68+
ip_packet->dest_ip >> 8 & 0xFF,
69+
ip_packet->dest_ip & 0xFF);
70+
}
71+
72+
int main() {
73+
struct ip_header ip_packet;
74+
75+
// 创建一个IP包
76+
create_ip_packet(&ip_packet, 0xC0A80001, 0xC0A80002); // 示例IP地址:192.168.0.1 -> 192.168.0.2
77+
78+
// 解析并打印IP包内容
79+
parse_ip_packet(&ip_packet);
80+
81+
return 0;
82+
}

server

15.9 KB
Binary file not shown.

server.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <arpa/inet.h>
2+
#include <netinet/in.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
#include <sys/socket.h>
7+
#include <unistd.h>
8+
9+
#define PORT 8080
10+
11+
int main() {
12+
int server_sock, new_sock;
13+
struct sockaddr_in addr;
14+
socklen_t addr_size = sizeof(addr);
15+
16+
// Create socket
17+
server_sock = socket(AF_INET, SOCK_STREAM, 0);
18+
if (server_sock == -1) {
19+
perror("Socket creation failed");
20+
exit(EXIT_FAILURE);
21+
}
22+
23+
// Set up address and port number
24+
addr.sin_family = AF_INET;
25+
addr.sin_addr.s_addr = INADDR_ANY;
26+
addr.sin_port = htons(PORT);
27+
28+
// Bind socket to address and port number
29+
if (bind(server_sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
30+
perror("Bind failed");
31+
exit(EXIT_FAILURE);
32+
}
33+
34+
// Listen for incoming connections
35+
listen(server_sock, 3);
36+
37+
printf("Server listening on port %d\\n", PORT);
38+
39+
while (1) {
40+
// Accept connection request from client
41+
addr_size = sizeof(addr);
42+
new_sock = accept(server_sock, (struct sockaddr *)&addr, &addr_size);
43+
if (new_sock < 0) {
44+
perror("Accept failed");
45+
exit(EXIT_FAILURE);
46+
}
47+
48+
printf("Connection accepted\\n");
49+
50+
// Close connection when done
51+
close(new_sock);
52+
}
53+
54+
return 0;
55+
}

server1

16.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)