-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client2.c
83 lines (70 loc) · 1.85 KB
/
Client2.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
//Syed Saifuddin
//L20410735
//Echo Server Program
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<arpa/inet.h>
int main(int argc , char *argv[])
{
int sock;
struct sockaddr_in server;
char message[1000], reply[1000];
/*Create a Socket*/
sock = socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1)
{
printf("**SOCKET NOT CREATED**\n");
}
else printf("**SOCKET READY**\n");
server.sin_addr.s_addr = inet_addr("127.0.0.1"); //IP Address
server.sin_family = AF_INET;
server.sin_port = htons(5004); //Port Number
/*Establish Connection to Server*/
if( connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
{
printf("**CONNECTION ERROR**\n");
return 1;
}
else printf("**CONNECTED**\n");
/*Reading from input11.dat*/
FILE *fp;
/* opening file for reading */
fp = fopen("input2.dat" , "r");
if(fp == NULL)
{
perror("**ERROR OPENING FILE**\n");
return(-1);
}
if( fgets (message,100, fp)!=NULL )
{
/* writing content to stdout */
printf("Sending to Server:");
puts(message);
}
fclose(fp);
/*Communicate with Server*/
/*Send your Message*/
if( send(sock , message , strlen(message) , 0) < 0)
{
printf("**Sending Error**\n");
return 1;
}
/*Reply from Server*/
if( recv(sock , reply , 2000 , 0) < 0)
{
printf("**Recieve Error**\n");
exit(0);
// break;
}
printf("Server reply:%s\n ",reply);
FILE *fp1 = fopen("output2.txt","w" );
if (fp1 != NULL)
{
fputs(reply, fp1);
fclose(fp1);
}
close (sock); //Close the socket
return 0;
}