Tag Archives: cpsc 471

C++ || Snippet – How To Send Text Over A Network Using A TCP Connection

The following is sample code which demonstrates the use of the “socket“, “connect“, “bind“, “read“, and “write” function calls for interprocess communication over a network on Unix based systems.

The following example demonstrates a simple network application in which a client sends text to a server, and the server replies (sends text) back to the client. This program demonstrates communication between two programs using a TCP connection.

Click here to examine the UDP version.

Note: The server program must be ran before the client program!

=== 1. SERVER ===

=== 2. CLIENT ===

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

The following is sample output.

SERVER OUTPUT:

Server started!

Waiting for someone to connect..

RECEIVED: "Server, what time is it?" from the client

SENDING: "Wed, Jan 08 2014, 07:51:53 PM" to the client

CLIENT OUTPUT:

SENDING: "Server, what time is it?" to the server

RECEIVED: "Wed, Jan 08 2014, 07:51:53 PM" from the server

C++ || Snippet – How To Send Text Over A Network Using A UDP Connection

The following is sample code which demonstrates the use of the “socket“, “bind“, “recvfrom“, and “sendto” function calls for interprocess communication over a network on Unix based systems.

The following example demonstrates a simple network application in which a client sends text to a server, and the server replies (sends text) back to the client. This program demonstrates communication between two programs using a UDP connection.

Click here to examine the TCP version.

Note: The server program must be ran before the client program!

=== 1. SERVER ===

=== 2. CLIENT ===


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

The following is sample output.

SERVER OUTPUT:

Server started!

Waiting for someone to connect..

RECEIVED: "Server, what time is it?" from the client

SENDING: "Wed, Jan 08 2014, 11:47:12 PM" to the client

CLIENT OUTPUT:

SENDING: "Server, what time is it?" to the server

RECEIVED: "Wed, Jan 08 2014, 11:47:12 PM" from the server

Java || Snippet – How To Send Text Over A Network Using Socket

The following is sample code which demonstrates the use of the “socket” function call for interprocess communication over a network.

The following example demonstrates a simple network application in which a client sends text to a server, and the server replies (sends text) back to the client.

Note: The server program must be ran before the client program!

=== 1. SERVER ===

=== 2. CLIENT ===


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

The following is sample output.

SERVER OUTPUT:

Server started!

Waiting for someone to connect..

(SRCIP=/127.0.0.1, SRCPORT=46608) (DESTIP=/127.0.0.1, DESTPORT=1234)

RECEIVED: "Server, what time is it?" from the client

SENDING: "Wed Jan 08 18:07:15 PST 2014" to the client

CLIENT OUTPUT:

SENDING: "Server, what time is it?" to the server

RECEIVED: "Wed Jan 08 18:07:15 PST 2014" from the server

Python || Snippet – How To Send Text Over A Network Using Socket, Send, & Recv

The following is sample code which demonstrates the use of the “socket“, “connect“, “send“, and “recv” function calls for interprocess communication over a network.

The following example demonstrates a simple network application in which a client sends text to a server, and the server replies (sends text) back to the client.

Note: The server program must be ran before the client program!

=== 1. SERVER ===

=== 2. CLIENT ===

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

The following is sample output.

SERVER OUTPUT:

Server started!

Waiting for someone to connect..

RECEIVED: "Server, what time is it?" from the client

SENDING: "Wed, Jan 08 2014, 04:02:13 PM" to the client

CLIENT OUTPUT:

SENDING: "Server, what time is it?" to the server

RECEIVED: "Wed, Jan 08 2014, 04:02:13 PM" from the server