CS计算机代考程序代写 FTP 60-256 System Programming: Sockets: examples

60-256 System Programming: Sockets: examples

Content

COMP 2560 System Programming:
Sockets: examples
Courtesy of Dr. B. Boufama
modified by Dan Wu
School of Computer Science University of Windsor

Instructor: Dr. Dan Wu

Sockets: examples
1

Copyright @ 2019, 2020, 2021 all rights reserved
Content

Content

1
Socket based client/server IPC

2

IP addresses and port numbers Port numbers
IP addresses

3

Examples: implementation of client/server applications Example 1: the server handles each client
Example 2: the server creates a child process for each client

Sockets: examples
2

Copyright @ 2019, 2020, 2021 all rights reserved
Socket based client/server IPC IP addresses and port numbers
Examples: implementation of client/server applications

Sockets: typical scenario

socket()
connect()
read/write
close()
Client

socket()
bind()
listen()
accept()
read/write
exchange message
Server
bind address
create end−point
specify queue
wait

Sockets: examples
3

Copyright @ 2019, 2020, 2021 all rights reserved
Socket based client/server IPC IP addresses and port numbers
Examples: implementation of client/server applications

Port numbers IP addresses

Port numbers

Port Numbers: Port numbers are 16 bit numbers used by TCP and UDP to identify different applications.
Three kinds of port numbers:

1

2

3
Well known ports: are those number from 0 – 1023. They are assigned by the IANA(Internet Assigned Numbers Authority), only be used by system processes. ftp server uses port 21, telnet server uses port 23, and web server uses port 80. Well-known port numbers are contained in file /etc/services on Unix machines.
Registered Ports: are from 1024 to 49151. They are registered by the I.A.N.A.
Dynamic and/or Private Ports: are from 49152 to 65535. Free of use.

Sockets: examples
4

Copyright @ 2019, 2020, 2021 all rights reserved
Socket based client/server IPC IP addresses and port numbers
Examples: implementation of client/server applications

Port numbers IP addresses

IP addresses

Each machine on the internet has a unique Internet Address(IP address). The address is made of 32 bit number, and normally written as four decimal numbers.
E.g. arc1 : 137.207.32.27
the command /usr/sbin/nslookup allows to get the IP address of a machine given its name (and vide versa).

Sockets: examples
5

Copyright @ 2019, 2020, 2021 all rights reserved
Socket based client/server IPC IP addresses and port numbers
Examples: implementation of client/server applications

Port numbers IP addresses

IP addresses

Sockets: examples
6

Remember casting?
Cast sockadd_in type to sockaddr type when…..?

Copyright @ 2019, 2020, 2021 all rights reserved
Socket based client/server IPC IP addresses and port numbers
Examples: implementation of client/server applications

Port numbers IP addresses

IP addresses and Port numbers

IP addresses and port numbers are integer values.

One problem we encounter when passing these values across
a network is that different hardware architectures store the bytes of a
multibyte integer in different orders.

The byte ordering used on a particular machine is called the host byte order

Sockets: examples
7

Copyright @ 2019, 2020, 2021 all rights reserved
Socket based client/server IPC IP addresses and port numbers
Examples: implementation of client/server applications

Port numbers IP addresses

IP addresses and Port numbers

Since port numbers and IP addresses must be transmitted
between, and understood by, all hosts on a network, a standard
ordering must be used. This ordering is called network byte order,
and happens to be big endian.

Sockets: examples
8

lscpu | grep “Byte Order”

Copyright @ 2019, 2020, 2021 all rights reserved
Socket based client/server IPC IP addresses and port numbers
Examples: implementation of client/server applications

Port numbers IP addresses

IP addresses and Port numbers

.

Sockets: examples
9

Copyright @ 2019, 2020, 2021 all rights reserved
Socket based client/server IPC IP addresses and port numbers
Examples: implementation of client/server applications

Example 1: the server handles each client
Example 2: the server creates a child process for each client

Example 1: Server

int main(int argc, char *argv[]){
char buffer[100] = “Hello, here is my message\n”; int sd, client;
socklen_t len;
struct sockaddr_in servAdd; //server socket address struct sockaddr_in cliAdd; //client socket address

sd = socket(AF_INET, SOCK_STREAM, 0); servAdd.sin_family = AF_INET; servAdd.sin_addr.s_addr = INADDR_ANY;
//INADDR_ANY allows your program to work without
// knowing the IP address of the machine it was running on servAdd.sin_port = 7777; // a port number
bind(sd,(struct sockaddr*)&servAdd,sizeof(servAdd)); listen(sd, 5);
while(1){
len = sizeof(cliAdd);
client=accept(sd,(struct sockaddr*)&cliAdd, &len); write(client, buffer, strlen(buffer) + 1); close(client);
}
}

Sockets: examples
10
server.c/client.c

Copyright @ 2019, 2020, 2021 all rights reserved
Socket based client/server IPC IP addresses and port numbers
Examples: implementation of client/server applications

Example 1: the server handles each client
Example 2: the server creates a child process for each client

Example 1: Client

int main(int argc, char *argv[]){ char buffer[100];
int server; socklen_t len;
struct sockaddr_in servAdd;//server socket address

server = socket(AF_INET, SOCK_STREAM, 0);

servAdd.sin_family = AF_INET; servAdd.sin_addr.s_addr = inet_addr(argv[1]);
//The inet_addr() function converts the specified string,
//in the Internet standard dot notation,
// to an integer value suitable for use as an Internet address.

servAdd.sin_port = 7777;

connect(server, (struct sockaddr *) &servAdd, sizeof(servAdd));

read(server, buffer, 100);

fprintf(stderr, “%s\n”, buffer);
}
Sockets: examples
11

Copyright @ 2019, 2020, 2021 all rights reserved
Socket based client/server IPC IP addresses and port numbers
Examples: implementation of client/server applications

Example 1: the server handles each client
Example 2: the server creates a child process for each client

Example 2: Server (parent process)

int main(int argc, char *argv[]){ char buffer[100];
int sd, cd; socklen_t len;
struct sockaddr_in servAdd, cliAdd;

sd = socket(AF_INET, SOCK_STREAM, 0); servAdd.sin_family = AF_INET; servAdd.sin_addr.s_addr = INADDR_ANY; servAdd.sin_port = 7777;
bind(sd,(struct sockaddr*)&servAdd,sizeof(servAdd)); listen(sd, 5);
while(1){
len = sizeof(cliAdd);
cd = accept(sd, (struct sockaddr *) &cliAdd, &len); if(fork()==0)
child(cd); close(cd);
}
}

Sockets: examples
12
server2.c/client2.c

Copyright @ 2019, 2020, 2021 all rights reserved
Socket based client/server IPC IP addresses and port numbers
Examples: implementation of client/server applications

Example 1: the server handles each client
Example 2: the server creates a child process for each client

Example 2: Server (child process)

void child(int sd){ char line[255];

while(1){
fprintf(stderr, “Enter a line to send client\n”); scanf(“%s”, line);
write(sd, line, strlen(line)+1); if(!read(sd, line, 255)){ close(sd);
exit(0);
}
fprintf(stderr, “Client sent back: %s\n”, line);
}
}

Sockets: examples
13

Copyright @ 2019, 2020, 2021 all rights reserved
Socket based client/server IPC IP addresses and port numbers
Examples: implementation of client/server applications

Example 1: the server handles each client
Example 2: the server creates a child process for each client

Example 2: Client

int main(int argc, char *argv[]){ char buffer[100];
int server; socklen_t len;
struct sockaddr_in servAdd; //server socket address

server = socket(AF_INET, SOCK_STREAM, 0);

servAdd.sin_family = AF_INET; servAdd.sin_addr.s_addr = inet_addr(argv[1]); servAdd.sin_port = 7777;

connect(server, (struct sockaddr *) &servAdd, sizeof(servAdd));

while(1){
read(server, buffer, 255);
fprintf(stderr, “Server’s message: %s\n”, buffer); fprintf(stderr, “Enter a line to send server\n”); scanf(“%s”, buffer);
if(buffer[0]==’$’){ close(server); exit(0);
}
write(server, buffer, strlen(buffer)+1);
}
}

Sockets: examples
14

Copyright @ 2019, 2020, 2021 all rights reserved
Socket based client/server IPC IP addresses and port numbers
Examples: implementation of client/server applications

Example 1: the server handles each client
Example 2: the server creates a child process for each client

Sockets: examples
15
Example 3: server3.c / client3.c
Example 4: server4.c / client4.c

Example 5: timeserv.c / timeclient.c
Example 6: rlsd.c / rls.c

Copyright @ 2019, 2020, 2021 all rights reserved
Socket based client/server IPC IP addresses and port numbers
Examples: implementation of client/server applications

Example 1: the server handles each client
Example 2: the server creates a child process for each client

Server/client socket programming Summary

Sockets: examples
16
socklib.c
(make_server_socket and connect_to_server functions)

Copyright @ 2019, 2020, 2021 all rights reserved
Socket based client/server IPC IP addresses and port numbers
Examples: implementation of client/server applications

Example 1: the server handles each client
Example 2: the server creates a child process for each client

Server/client socket programming Summary

Sockets: examples
17
Server normally looks like:

Copyright @ 2019, 2020, 2021 all rights reserved
Socket based client/server IPC IP addresses and port numbers
Examples: implementation of client/server applications

Example 1: the server handles each client
Example 2: the server creates a child process for each client

Server/client socket programming Summary

Sockets: examples
18
Client normally looks like:

Copyright @ 2019, 2020, 2021 all rights reserved
Socket based client/server IPC IP addresses and port numbers
Examples: implementation of client/server applications

Example 1: the server handles each client
Example 2: the server creates a child process for each client

Our previous time server program could look like:

Sockets: examples
19

Copyright @ 2019, 2020, 2021 all rights reserved
Socket based client/server IPC IP addresses and port numbers
Examples: implementation of client/server applications

Example 1: the server handles each client
Example 2: the server creates a child process for each client

Another way to do it:

Sockets: examples
20

wait or not wait?

Copyright @ 2019, 2020, 2021 all rights reserved