-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCPserver.c
More file actions
82 lines (67 loc) · 1.73 KB
/
Copy pathTCPserver.c
File metadata and controls
82 lines (67 loc) · 1.73 KB
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
#include<stdio.h>
#include<strings.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<math.h>
#include<netinet/in.h>
void main(int argc, char const *argv[])
{
int sockfd, clientLength, connfd;
sockfd=socket(AF_INET, SOCK_STREAM,0);
if(sockfd>0)
printf("\nSocket created successfully");
struct sockaddr_in servaddr;
struct sockaddr_in clientaddr;
servaddr.sin_family=AF_INET;
servadrr.sin_addr.s_addr=INADDR_ANY;
servaddr.sin_port=6006;
if(bind(sockfd,(struct socaddr *)&servaddr, sizeof(servaddr))==0)
printf("\n Bind successfull");
if(listen(sockfd,5)==0){
printf("\nSocket listening for connection..");
}
clientLength=sizeof(clientaddr);
connfd=accept(sockfd,(struct socaddr *)&clientaddr,&clientLength);
if(connfd>0){
printf("\nConnection accepted successfully");
}
//Reading message
char msg[25];
read(connfd,&msg,sizeof(msg));
printf("%s\n",msg);
//send a file
FILE *fp;
char tempChar;
fp=fopen("a.txt","r");
tempChar=fgetc(fp);
while(tempChar!=EOF){
write(connfd,&tempChar,sizeof(tempChar));
ch=fgetc(fp);
}
write(connfd,&tempChar,sizeof(tempChar));
printf("\nFILE SENT");
fclose();
//Maths operations
char operator;
int operand1, operand2, result;
read(connfd,&operator,sizeof(operator));
read(connfd,&operand1,sizeof(operand1));
read(connfd,&operand2,sizeof(operand2));
switch(operator){
case '+':result=operand1+operand2;
break;
case '-':result=operand1-operand2;
break;
case '*':result=operand1*operand2;
break;
case '/':result=operand1/operand2;
break;
default:
}
printf("Result : %d %c %d = %d\n",operand1,operator,operand2,result );
write(connfd,&result,sizeof(result));
//trigonometric oprations
close(sockfd);
}