Repository navigation
Expand file tree
/
Copy pathserver1.cpp
More file actions
75 lines (65 loc) · 1.64 KB
/
Copy pathserver1.cpp
File metadata and controls
75 lines (65 loc) · 1.64 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
#include <iostream>
using namespace std;
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
// server receives a single character from the client('a') and it sends 'b' to client
int main()
{
//socket creation
int serv_fd = socket(AF_INET,SOCK_STREAM,0);
if (serv_fd == -1)
{
perror("socket creation error");
exit(1);
}
//initialize the socaddr_in structure
struct sockaddr_in sock_addr_serv;// sockaddr_in for IPV4
sock_addr_serv.sin_family = AF_INET;
sock_addr_serv.sin_port = 9988;
sock_addr_serv.sin_addr.s_addr = inet_addr("127.0.0.1");
//bind the socket with ip address and port
if (bind(serv_fd,(struct sockaddr *)&sock_addr_serv,sizeof(sock_addr_serv))==-1)
{
perror("bind error");
exit(1);
}
// listening (waiting) for client request
if (listen(serv_fd,2)==-1)
{
perror("listen error");
exit(1);
}
while (1)
{
cout << "server waiting for client to connect" << endl;
//accepting client request
struct sockaddr_in sock_addr_cli;
socklen_t cli_len = sizeof(sock_addr_cli);
int client_fd=accept(serv_fd,(struct sockaddr *)&sock_addr_cli,&cli_len);
cout << "in server serv fd" << serv_fd << endl;
cout << "in server client fd" << client_fd << endl;
cout << "in server port (50) " << sock_addr_cli.sin_port << endl;
cout << "in server ip " << sock_addr_cli.sin_addr.s_addr << endl;
if (client_fd==-1)
{
perror(" accept error");
exit(1);
}
char ch;
read(client_fd,&ch,1);
cout << " from client : " << ch << endl;
ch++;
write(client_fd,&ch,1);
}
//close socket
if (close(serv_fd)==-1)
{
perror("socket close error");
exit(1);
}
}