-
Notifications
You must be signed in to change notification settings - Fork 1
/
UDP chatapp.txt
84 lines (77 loc) · 1.66 KB
/
UDP chatapp.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
//CHAT APPLICATION
// UDP CLIENT
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#define port 5000
void main()
{
int clisocket;
struct sockaddr_in serveraddr;
struct sockaddr_in newaddr;
while(1)
{
clisocket=socket(PF_INET,SOCK_DGRAM,0);
if(clisocket>0)
{
printf("socket created\n");
}
serveraddr.sin_family=PF_INET;
serveraddr.sin_port=htons(port);
serveraddr.sin_addr.s_addr=inet_addr("127.0.0.1");
char mesg[100];
char buff[100];
printf("Enter message :");
scanf("%s",mesg);
printf("\nEnter password :");
if(msg[0]=='0')
{
break;
}
send(clisocket,mesg,sizeof(msg),0);
recv(clisocket,buff,sizeof(buff),0);
printf("data received is %s",buff);
close(clisocket);
}
}
//SERVER:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<sys/socket.h>
#define port 5000
void main()
{
int sersocket,newsocket,s,clisocket;
struct sockaddr_in *serveraddr;
struct sockaddr_in *newaddr;
sersocket=socket(PF_INET,SOCK_STREAM,0);
if(sersocket>0)
{
printf("socket is created");
}
serveraddr.sin_family=PF_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("bind success");
clisocket=sizeof(newaddr);
int n=0;
char buffer[100];
char suff[100];
while(n=recv(sersocket,buffer,sizeof(buffer))>0)
{
printf("message from client is %s",buffer);
printf("send");
scanf("%s",suff);
send(sersocket,suff,sizeof(suff),0);
}
close(sersocket);
}