-
Notifications
You must be signed in to change notification settings - Fork 1
/
slide_server.c
66 lines (61 loc) · 1.3 KB
/
slide_server.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
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
#include<stdlib.h>
#include<arpa/inet.h>
#define SIZE 4
int main()
{
int sfd,lfd,len,i,j,status;
char str[20],frame[20],temp[20],ack[20];
struct sockaddr_in saddr,caddr;
sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd<0)
perror("Error");
bzero(&saddr,sizeof(saddr));
saddr.sin_family=AF_INET;
saddr.sin_addr.s_addr=htonl(INADDR_ANY);
saddr.sin_port=htons(5000);
if(bind(sfd,(struct sockaddr*)&saddr,sizeof(saddr))<0)
perror("Bind Error");
listen(sfd,5);
len=sizeof(&caddr);
lfd=accept(sfd,(struct sockaddr*)&caddr,&len);
printf("Enter the text to send : ");
scanf("%s",str);
i=0;
printf("\n\n");
while(i<strlen(str))
{
memset(frame,0,20);
strncpy(frame,str+i,SIZE);
len=strlen(frame);
for(j=0;j<len;j++)
{
sprintf(temp,"%d",i+j);
strcat(frame,temp);
}
printf("Frame transmitted : %s\n",frame);
write(lfd,frame,sizeof(frame));
read(lfd,ack,20);
sscanf(ack,"%d",&status);
if(status==-1)
{
printf("-Transmission is successful. \n");
i = i + SIZE ;
}
else
{
printf("-Received error in %d. \n",status);
i = status ;
}
printf("\n");
}
write(lfd,"exit",sizeof("exit"));
printf("Exiting\n");
sleep(2);
close(lfd);
close(sfd);
}