-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat_client.c
155 lines (122 loc) · 2.75 KB
/
chat_client.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<errno.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<pthread.h>
pthread_t thread;
void error (char *msg){
perror(msg);
exit(0);
}
void* receive(void* d)
{
int sockfd=*((int *)d);
int a;
char buf[256];
while(1)
{
bzero(buf,256);
a=recv(sockfd,buf,255,0);
if(a<=0)
error("Error reading from socket");
else
printf("\n %s\n ",buf);
}
close(sockfd);
pthread_exit(NULL);
}
int main (int argc, char *argv[]) {
int sockfd, portno, n,newsockfd,userno;
char user[100],pass[30];
struct sockaddr_in addr_in, serv_addr;
struct hostent *server;
char buffer[256];
FILE *file;
if (argc<3) {
fprintf (stderr,"Usage %s Host Name Port\n", argv[0]);
exit(0);
}
portno=atoi(argv[2]);
sockfd=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sockfd<0)
error("ERROR Opening Socket");
server=gethostbyname(argv[1]);
if (server==NULL) {
fprintf(stderr, "ERROR, No Such Host\n");
exit(0);
}
bzero((char*)&serv_addr, sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
bcopy((char*)server->h_addr, (char*)&serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port=htons(portno);
newsockfd = connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr));
if (newsockfd<0)
error("ERROR Connecting");
else
{
reg:
bzero(user,100);
username:
printf("\nENTER THE USERNAME : ");
fgets(user,100,stdin);
if(strncmp(user,"user-",5)!=0)
{
printf("USERNAME FORMAT NOT CORRECT (FORMAT - user-<no>)");
goto username;
}
n=send(sockfd, user, strlen(user), 0);
printf("\nENTER THE PASSWORD : ");
fgets(pass,30,stdin);
n=send(sockfd, pass, strlen(pass), 0);
bzero(buffer,256);
n=recv(sockfd,buffer,255,0);
if(n>0)
printf("%s\n",buffer);
if(strcmp(buffer,"USERNAME EXISTS")==0)
goto reg;
}
file=fopen("user.txt","a+");
char str[100];
bzero(str,100);
printf("\nLOGGED IN USERS\n");
while(fscanf(file,"%s",str)!=EOF)
{
if(str[0]=='u')
printf("\n%s\n",str);
fscanf(file,"%s",str);
}
if(strcmp(buffer,"USER REGISTERED")==0)
{
fprintf(file,"%s",user);
fprintf(file,"\n%s\n",pass);}
fclose(file);
pthread_create(&thread,NULL,receive,(void*)&sockfd);
while(1)
{
enter:
printf("\nPlease Enter The Message: ");
bzero(buffer, 256);
fgets(buffer, 255, stdin);
if((strncmp(buffer,"To user-",8)==0)||(strncmp(buffer,"bye",3)==0))
{
sprintf(buffer,"%s|%s",buffer,user);
n=send(sockfd, buffer, strlen(buffer), 0);
if(n<0)
error("ERROR Writing To Socket");
if(strncmp(buffer,"bye",3)==0)
break;
}
else
{
printf("\nMESSAGE FORMAT NOT CORRECT\n");
goto enter;}
}
close(sockfd);
return 0;
}