-
Notifications
You must be signed in to change notification settings - Fork 1
/
multichat.txt
111 lines (76 loc) · 1.93 KB
/
multichat.txt
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
CLIENT - SERVER CHAT APPLICATION
CLIENT(TCP):
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
#define port 5000
void main()
{
struct sockaddr_in serveraddr;
struct sockaddr_in newaddr;
int clisocket;
int size;
char buffer[100];
while(1)
{
clisocket=socket(AF_INET,SOCK_STREAM,0);
if(clisocket>0)printf("socket created");
serveraddr.sin_family=AF_INET;
serveraddr.sin_port=htons(port);
serveraddr.sin_addr.s_addr=inet_addr("127.0.0.1");
connect(clisocket,(struct sockaddr *)&serveraddr,sizeof(serveraddr));
printf("Enter message to send to server :");
scanf("%s",buffer);
if(buffer[0]=='0')
{
break;
}
send(clisocket,sizeof(buffer),1024,0);
recv(clisocket,sizeof(buffer),1024,0);
printf("\n Received from server: %s",buffer);
close(clisocket);
}
}
SERVER(TCP)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<sys/socket.h>
#define port 5000
void main()
{
int sersocket,s,newsocket,clisocket;
struct sockaddr_in serveraddr;
struct sockaddr_in newaddr;
sersocket=socket(AF_INET,SOCK_STREAM,0);
if(sersocket>0) printf("socket created");
serveraddr.sin_family=AF_INET;
serveraddr.sin_port=htons(port);
serveraddr.sin_addr.s_addr=htonl(INADDR_ANY);
s=bind(sersocket,(struct sockaddr *)&serveraddr,
sizeof(serveraddr))
if(s==0) printf("\nsocket is binded");
listen(sersocket,1);
clisocket=sizeof(newaddr);
int n;
char buffer[100];
while((newsocket=accept(sersocket,(struct sockaddr *)&newaddr,&clisocket))>0)
{
while((n=recv(newsocket,buffer,sizeof(buffer,0))>0)
{
printf("data received by server %s",buffer);
printf("\nenter word to be sent to client ");
scanf("%s",buffer);
send(newsocket,buffer,sizeof(buffer),0);
}
}
close(sersocket);
//close(newsocket);
}