-
Notifications
You must be signed in to change notification settings - Fork 0
/
webclient.c
73 lines (49 loc) · 1.48 KB
/
webclient.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
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include <arpa/inet.h>
#include <unistd.h>
int main(){
int socketFD;
if ((socketFD= socket (AF_INET,SOCK_STREAM,0))<0){
perror("error creating socket:");
return 0;
}
printf("sucessfully created socket fd %i, continuing\n",socketFD);
struct sockaddr_in address;
memset((void*)&address,0,sizeof(address)); //this struct is already defined in the sys/socket.h file
address.sin_family=AF_INET;
if(bind(socketFD,(struct sockaddr *)&address,sizeof(address))<0){
perror("error binding:");
}
printf("binding ip to port was sucessfull, continuing \n");
struct sockaddr_in dest;
dest.sin_family=AF_INET;
dest.sin_port=htons(20001);
char *ip="41.89.68.78";
inet_pton(AF_INET,ip,&dest.sin_addr);
if (connect(socketFD,(struct sockaddr *)&dest,sizeof(dest)) ==0){
printf("connection was successfull\n");
}
printf("your message\n");
printf("------------\n");
char *buff=NULL;
size_t len=0;
ssize_t msgcount;
while (true)
{
ssize_t msgcount=getline(&buff,&len,stdin);
printf("------------\n");
if (msgcount>0){
if (strcmp(buff,"exit\n")==0){
break;
}
ssize_t msgsent=send(socketFD,buff,msgcount,0);
}
}
close(socketFD);
return 0;
}