代写代考 WIN32__

IPv6 ready
Cross-platform
Multi-threading
Socket API (C Language)

Copyright By PowCoder代写 加微信 powcoder

For code compatibility, as much as possible, we will be using data types and functions that are compatible to both Windows and Unix-based OS (i.e. Linux, MacOS, etc.)

Multithreaded TCP Server
Listening Socket (TCP)
When the client bids good bye, the sockets created at both sides (new socket at the server and the client¡¯s socket) will all be closed.
Concurrent TCP Server

1. In TCP, the Server should be running already prior to a Client connecting to it
The thread at the server will also be terminated. Nevertheless, the server will keep on listening for any incoming clients.
2. Upon accepting a client, the server creates a new socket and a new thread exclusive for that client.
3. Communication session begins: server and client may exchange messages to each

Listening Socket
New socket for C1
Concurrent TCP Server
Multiple clients may communicate with the server simultaneously.
New socket for C2
The server will create a new socket and a new thread for each client to facilitate the concurrent communication session. The server will also keep state information for
each of the clients.

Concurrent TCP Server
Creating a thread using _beginthread()
#include //_beginthread, _endthread
Per client communication session is run inside a thread
The server creates a new socket and a new thread (using _beginthread) for each client to facilitate the concurrent communication session.

Concurrent TCP Server
Creating a thread using _beginthread()
The communication session is implemented inside a function that takes in a structure parameter.
p is a structure containing a socket and address information

Communication session
Concurrent TCP Server
The communication session runs a loop to allow the client to interact with the server indefinitely long.
The input argument is a structure containing a socket and address information
Send reply to client
When a client sends a ¡°bye¡± command, the server terminates the session.
server terminates the session by breaking out of the loop, closing the
socket, then terminating the thread

Concurrent TCP Server
Demo: TCP server interacting with multiple TCP clients.
The client may terminate its connection with the server by sending a ¡°bye¡± command.
A single dot (.) input from the keyboard terminates the TCP client.

Cross-Platform, IPv6-ready version
Main differences only
For the complete source codes, please download the client, server codes on Stream:
TCP_server_client_CrossPlatform_IPv6File.zip

Cross-platform, IPv6 test
I have tested the cross-platform codes using Windows and Linux machines running on a private network that supports IPv6 routing.

Headers required
#elif defined __WIN32__
System-specific Predefined Macros
Cross-Platform version
#if defined __unix__ || defined __APPLE__
_WIN32 is defined even when targeting the Windows x64 version

Headers required
#elif defined _WIN32
Cross-Platform version
#if defined __unix__ || defined __APPLE__
#include
#include
#include //Structures and functions used for socket API
#include // Structures and functions used for socket API
#include
#include //used for domain/DNS hostname lookup (e.g getnameinfo())
#include
#include //required by getnameinfo() , getaddrinfo() and special constants #define WSVERS MAKEWORD(2,2)

Cross-Platform version
Socket variable declaration
#if defined __unix__ || defined __APPLE__
int s, ns;
#elif defined _WIN32
SOCKET s, ns; #endif
Socket variable initialisation
#if defined __unix__ || defined __APPLE__
#elif defined _WIN32
s = INVALID_SOCKET; #endif

Socket creation
#elif defined _WIN32
Cross-Platform version
s = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
#if defined __unix__ || defined __APPLE__
Applicable in Linux, Mac OS and Windows operating systems
if (s < 0) { printf("socket failed\n"); freeaddrinfo(result); exit(1); //check for errors in socket allocation if (s == INVALID_SOCKET) { printf("Error at socket(): %d\n", WSAGetLastError()); freeaddrinfo(result); WSACleanup(); Cross-Platform version Binding a socket to an IP address and port number // bind the TCP welcome socket to the local address of the machine and port number iResult = bind( s, result->ai_addr, (int)result->ai_addrlen);
//if error is detected, then clean-up
Applicable in Linux, Mac OS and Windows operating systems
#if defined __unix__ || defined __APPLE__
if (iResult == -1) {
printf( “\nbind failed\n”); freeaddrinfo(result); close(s);//close socket
#elif defined _WIN32
if (iResult == SOCKET_ERROR) {
printf(“bind failed with error: %d\n”, WSAGetLastError()); freeaddrinfo(result);
closesocket(s);
WSACleanup();
Only applies in Windows
exit(1); }
freeaddrinfo(result);

#elif defined _WIN32
Cross-Platform version
#if defined __unix__ || defined __APPLE__
if (listen( s, SOMAXCONN) == -1) { #elif defined _WIN32
if (listen( s, SOMAXCONN ) == SOCKET_ERROR ) { #endif
#if defined __unix__ || defined __APPLE__
printf( “\nListen failed\n”);
printf( “Listen failed with error: %d\n”, WSAGetLastError() );
closesocket(s);
WSACleanup();
exit(1); } else {
printf(“\n<<>> is listening at PORT: %s\n”, portNum);

#elif defined _WIN32
Cross-Platform version
bytes = send(ns, send_buffer, strlen(send_buffer), 0);
#if defined __unix__ || defined __APPLE__
Applicable in Linux, Mac OS and Windows operating systems
if ((bytes == -1) || (bytes == 0)) { break;
if ((bytes == SOCKET_ERROR) || (bytes == 0)) { break;

#elif defined _WIN32
Cross-Platform version
bytes = recv(s, &receive_buffer[n], 1, 0);
#if defined __unix__ || defined __APPLE__
Applicable in Linux, Mac OS and Windows operating systems
if ((bytes == -1) || (bytes == 0)) { printf(“recv failed\n”);
if ((bytes == SOCKET_ERROR) || (bytes == 0)) { printf(“recv failed\n”);

References
Windows Socket library
https://docs.microsoft.com/en-us/windows/win32/api/_winsock/
http://long.ccaba.upc.edu/long/045Guidelines/eva/ipv6.html#daytimeServer6 http://long.ccaba.upc.edu/
Beej Guide to Network Programming Using Internet Sockets Andre Barczak¡¯s TCP server-client codes

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com