-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
136 lines (107 loc) · 3.61 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//
// main.c
// IP protocol in C
//
// Created by Nikolay Stoyanov on 12.06.24.
//
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdbool.h>
#include <pthread.h>
int server(void* unused) {
// insert code here...
int socket_desc, client_sock, client_size;
struct sockaddr_in server_addr, client_addr;
char server_message[2000], client_message[2000];
//Clean buffers
memset(server_message, '\0', sizeof(server_message));
memset(client_message, '\0', sizeof(client_message));
//Create socket
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if(socket_desc < 0){
printf("Error while createing a socket \n");
return -1;
}
printf("Socket created successfully\n");
//Set port and ip
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(2000);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
//bind to the set port and IP
if(bind(socket_desc, (struct sockaddr*)&server_addr, sizeof(server_addr))<0){
printf("Couldn't bind to the port \n");
return -1;
}
printf("Done with bindng\n");
//listen for clients:
if(listen(socket_desc, 1) < 0){
printf("Error while listening \n");
return -1;
}
printf("\nListening for incoming connections...\n");
//Accept of incoming connection
client_size = sizeof(client_addr);
client_sock = accept(socket_desc, (struct sockaddr*)&client_addr, &client_size);
if (client_sock < 0){
printf("Can't accept\n");
return -1;
}
printf("Client connected at IP: %s and port: %i\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
// Closing the socket:
// close(client_sock);
// close(socket_desc);
return 0;
}
int client(void* unused){
int socket_desc;
struct sockaddr_in server_addr;
char server_message[100], client_message[100];
// Clean buffers:
memset(server_message,'\0',sizeof(server_message));
memset(client_message,'\0',sizeof(client_message));
// Create socket:
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if(socket_desc < 0){
printf("Unable to create socket\n");
return -1;
}
printf("Socket created successfully\n");
// Set port and IP the same as server-side:
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(2000);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
// Send connection request to server:
if(connect(socket_desc, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0){
printf("Unable to connect\n");
return -1;
}
printf("Connected with server successfully\n");
// Get input from the user:
//printf("Enter message: ");
//scanf("%s", client_message);
// Send the message to server:
if(send(socket_desc, "Message from client", strlen("Message from client"), 0) < 0){
printf("Unable to send message\n");
return -1;
}
// Receive the server's response:
if(recv(socket_desc, server_message, sizeof(server_message), 0) < 0){
printf("Error while receiving server's msg\n");
return -1;
}
printf("Server's response: %s\n",server_message);
// Close the socket:
//close(socket_desc);
return 0;
}
int main(int argx, const char * argv[]){
pthread_t serv, cl;
pthread_create(&serv, NULL, server, NULL);
pthread_create(&cl, NULL, client, NULL);
void* unused;
pthread_join(serv, &unused);
pthread_join(cl, &unused);
return 0;
}