-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
63 lines (58 loc) · 1.58 KB
/
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
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <string.h>
extern int errno;
int port;
int main (int argc, char *argv[])
{
int sd,ok=1;
struct sockaddr_in server;
char msg[10000];
if (argc != 3)
{
printf ("Sintaxa: %s <adresa_server> <port>\n", argv[0]);
return -1;
}
port = atoi (argv[2]);
if ((sd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
{
perror ("Eroare la socket().\n");
return errno;
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(argv[1]);
server.sin_port = htons (port);
if (connect (sd, (struct sockaddr *) &server,sizeof (struct sockaddr)) == -1)
{
perror ("Eroare la connect().\n");
return errno;
}
while(ok)
{
bzero (msg, 200);
printf ("Introduceti o comanda: ");
fflush (stdout);
read (0, msg, 200);
//printf("[%s]\n",msg);
if(!strcmp(msg,"quit\n") || !strcmp(msg,"Quit\n") || !strcmp(msg,"quit") || !strcmp(msg,"Quit")) ok=0;
if (write (sd, msg, 200) <= 0)
{
perror ("Eroare la write() spre server.\n");
return errno;
}
if (read (sd, msg, 2500) < 0)
{
perror ("Eroare la read() de la server.\n");
return errno;
}
printf ("Mesajul primit este:\n%s\n", msg);
if(ok==0) close(sd);
}
close (sd);
}