-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.cpp
More file actions
87 lines (79 loc) · 1.62 KB
/
Copy pathclient.cpp
File metadata and controls
87 lines (79 loc) · 1.62 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
83
84
85
86
87
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<string.h>
#include<stdlib.h>
#include<netinet/in.h>
#define PORT 8000
#define MAXSZ 100
static const char key2[]="HIGHUH";
void encrypt(char* input, char key[])
{
int k=0;
for(int i=0;i<6;i++)
{
input[i]=input[i]^key[i];
cout<<input[i];
}cout<<endl;
}
void decrypt(char* input,char key[])
{
for(int i=0;i<6;i++)
{
input[i]=input[i]^key[i];
cout<<input[i];
}
}
int main()
{
int sockfd;
int newsockfd;
struct sockaddr_in serverAddress;
struct sockaddr_in clientAddress;
int n;
char msg[MAXSZ];
int clientAddressLength;
int pid;
sockfd=socket(AF_INET,SOCK_STREAM,0);
memset(&serverAddress,0,sizeof(serverAddress));
serverAddress.sin_family=AF_INET;
serverAddress.sin_addr.s_addr=htonl(INADDR_ANY);
serverAddress.sin_port=htons(PORT);
bind(sockfd,(struct sockaddr *)&serverAddress, sizeof(serverAddress));
listen(sockfd,5);
while(1)
{
printf("Server waiting for new client connection: ");
clientAddressLength=sizeof(clientAddress);
newsockfd=accept(sockfd,(struct sockaddr*)&clientAddress,&clientAddressLength);
printf("Connected to client: %s\n",inet_ntoa(clientAddress.sin_addr));
pid=fork();
if(pid==0)
{
char input[100000];
while(1)
{
n=recv(newsockfd,input,MAXSZ,0);//RECV_2
if(n==0)
{
close(newsockfd);
break;
}
encrypt(input,key2);
send(newsockfd,input,n,0);//SEND
n=recv(newsockfd,input,MAXSZ,0);//RECV_2
if(n==0)
{
close(newsockfd);
break;
}
decrypt(input,key2);
printf("Received: %s\n",input);
} exit(0);
}
else
{
close(newsockfd); }
}
return 0;
}