留学生作业代写 IT027049.massey.ac.nz:0 LISTENING TCP IT027049:epmap IT027049.massey.ac.

IPv6 ready
Cross-platform
Socket API (C Language)
Goal: learn how to build client/server applications that communicate using sockets

Copyright By PowCoder代写 加微信 powcoder

Getting Started…
WinSock Functions
Predefined constants and error codes
Special Structures, Unions and data types suited for socket programming
Correct order of function calls to synchronize Server and Client applications

Socket API
Two types of Internet Sockets:
(a “door”) into which application process can both send and receive messages to/from another (remote or local) application process TCP/UDP
Socket Programming
 introduced in BSD4.1 UNIX, 1981
 Sockets are explicitly created, used
and then released by applications
a host-local, application-created/owned, OS-controlled interface
 uses client/server paradigm
 has evolved through the years and is now part of every major OS
1. (TCP socket) reliable, byte stream- oriented
2. (UDP socket) unreliable datagram
 WinSock 2 – to provide a protocol- independent transport interface that is fully capable of supporting emerging networking capabilities including real- time multimedia communications
https://docs.microsoft.com/en-us/windows/win32/winsock/what-s-new-for-windows-sockets-2

Transmission Control Protocol

Socket Programming using TCP TCP service: reliable transfer of bytes from one process
to another
controlled by application developer
controlled by application developer
controlled by operating system
TCP with buffers, variables
TCP with buffers, variables
controlled by operating system
client or server
client or server

Simple Client-Server Application

Socket Programming with TCP
Server must be always “on”
 server process must be running first
 server must have created socket (“door”) that welcomes
client’s contact
“Client knocking on the door”
Server socket (associated with a port number)

Socket Programming with TCP
A socket is bound to a port
Listening Socket
number so that the transport
layer can identify the
Connection request
Port number
application process to deliver S data to.
1. IPaddress–identifiesthemachine running the server process.
2. Portnumber–identifiesthespecific process in the machine.
IP address

Socket Programming with TCP Listening Socket
Connection request
Port number
C Socket IP address
Client contacts server by:
creating client-local TCP socket specifying IP address, port
number of server process
IP address

IP address

IP address
Socket Programming with TCP
Application Viewpoint
Listening Socket
TCP provides reliable, in-order
transfer of bytes (“pipe”)
between a client and a server.
Connection request
Port number
 When contacted by client, server TCP creates new socket for server process to communicate with client
 Multithreading allows server to talk to multiple clients back

Simple example of a client-server application
Communication session:
1. Client reads line from standard input
2. Client sends to server via socket
3. Server reads line from socket
4. Server counts the number of bytes contained in the line, then sends a message back to the client
5. Client reads the reply, then prints the message received from the server
6. Repeat steps 1-5 until client decides to quit.
The client quits as soon as we type a single dot (.)
Let us look at a demo first… (client and server codes are
downloadable in Stream)

Simple TCP Server Compilation Download the TCP server codes from our stream website,
then compile the codes using the makefile provided.

Simple TCP Client Compilation Download the TCP client codes from our stream website,
then compile the codes using the makefile provided.

serverWindows 1155
clientWindows localhost 1155
Refers to the IP address of the local machine (since the server is running in the same machine as the client)
Port number of the server
Port number (any ephemeral port number will do: [1024, 65535] )

Simple TCP Client-Server Interaction

Client/server socket interaction: TCP
Server (running on hostid)
create socket, port=x, for checking incoming request:
welcomeSocket = socket()
wait for incoming connection request
TCP connection setup
create socket,
connect() to hostid, port=x
connectionSocket = accept() receive request from
clientSocket = socket()
connectionSocket
clientSocket
send reply to
connectionSocket
receive reply from
connectionSocket
clientSocket
send request using
clientSocket
Application 2-19

Proper Ordering of Send and Recv
socket welcomingSocket bind to listening port number listen on welcomingSocket loop “forever”
{ //main loop
socket clientSocket connect
accept /* by creating newsocket */ /* communicate with client*/
loop until done { //inner loop
closesocket(clientSocket)
*this is not an ordinary sequential program.
} //end of inner loop
listen() runs in the background and stays running until the socket is closed.
closesocket(newsocket) } //end of main loop
accept() causes the program to wait back
closesocket(welcomingSocket)
/* communicate with server*/
loop until done {

outToServer inFromUser
inFromServer
Stream jargon
TCP is a byte-stream protocol.
 stream is a sequence of characters that flow into or out of a process.
input stream
 input stream is attached to some input source for the process, e.g., keyboard or socket.
output stream
input stream
 output stream is attached to an output source, e.g., monitor or socket.
TCP socket
Client process
client TCP
to network
from network
clientSocket
Application 2-22

Network Tool Netstat
Which ports are currently in use, and which ones are available to our program?

Netstat -a
Active Connections
Some utility: Netstat
ForeignAddress State IT027049.massey.ac.nz:0 LISTENING TCP IT027049:epmap IT027049.massey.ac.nz:0 LISTENING
Proto LocalAddress TCP IT027049:http
Displays all active TCP connections and the TCP and UDP ports on which the computer is listening
TCP IT027049:https IT027049.massey.ac.nz:0 LISTENING TCP IT027049:microsoft-ds IT027049.massey.ac.nz:0 LISTENING
IT027049.massey.ac.nz:0 LISTENING IT027049.massey.ac.nz:0 LISTENING IT027049.massey.ac.nz:0 LISTENING IT027049.massey.ac.nz:0 LISTENING IT027049.massey.ac.nz:0 LISTENING IT027049.massey.ac.nz:0 LISTENING IT027049.massey.ac.nz:0 LISTENING IT027049.massey.ac.nz:0 LISTENING IT027049.massey.ac.nz:0 LISTENING IT027049.massey.ac.nz:0 LISTENING
TCP IT027049:1025
TCP IT027049:1179
TCP IT027049:1181
TCP IT027049:1300
TCP IT027049:1318
TCP IT027049:1786
TCP IT027049:1787
TCP IT027049:1790
TCP IT027049:1791
TCP IT027049:5000
TCP IT027049:13450
TCP IT027049:netbios-ssn IT027049.massey.ac.nz:0 LISTENING
TCP IT027049:1082 TCP IT027049:1179 TCP IT027049:1181 TCP IT027049:1318 TCP IT027049:1456 TCP IT027049:1456 TCP IT027049:1467 TCP IT027049:1467 TCP IT027049:1786
IT027049.massey.ac.nz:0 LISTENING its-xchg4.massey.ac.nz:1165 ESTABLISHED its-dc2.massey.ac.nz:1025 ESTABLISHED hnt-up-dhcp-494.wharton.upenn.edu:62686 ESTABLISHED IT027049.massey.ac.nz:0 LISTENING alb-file2.massey.ac.nz:netbios-ssn ESTABLISHED IT027049.massey.ac.nz:0 LISTENING itsa-campus1.massey.ac.nz:netbios-ssn ESTABLISHED d226-94-36.home.cgocable.net:7091 ESTABLISHED
IT027049.massey.ac.nz:0 LISTENING

TCP IT027049:1787 TCP IT027049:1790 TCP IT027049:1791 TCP IT027049:8947
pcp0011009611pcs.detrtc01.mi.comcast.net:21848 ESTABLISHED balticom-132-227.balticom.lv:63567 ESTABLISHED 24-29-117-20.nyc.rr.com:1236 ESTABLISHED IT027049.massey.ac.nz:0 LISTENING
UDP IT027049:microsoft-ds *:* UDP IT027049:isakmp *:* UDP IT027049:1026 *:* UDP IT027049:1027 *:* UDP IT027049:1028 *:* UDP IT027049:1046 *:* UDP IT027049:1088 *:* UDP IT027049:1177 *:* UDP IT027049:13450 *:* UDP IT027049:38037 *:* UDP IT027049:ntp *:* UDP IT027049:1187 *:* UDP IT027049:1459 *:* UDP IT027049:1718 *:* UDP IT027049:1900 *:* UDP IT027049:ntp *:* UDP IT027049:netbios-ns *:* UDP IT027049:netbios-dgm *:* UDP IT027049:1900 *:* UDP IT027049:8760 *:* UDP IT027049:62493 *:*
Protocol Port UDP 137
Description
Explanation
NETBIOS-NS NETBIOS name service

Allow access to your server

Hostname, IP addresses
 The server could be referred to by many alternative host names and IP addresses, especially when testing the client-server programs on the same machine.
 Invoke the network tool named ipconfig /all to see the host name assigned to your machine.
• IT043429.massey.ac.nz //host name assigned to my machine
• localhost //refers to IP address of the local machine
• loopback //same as localhost
• 127.0.0.1 //same as loopback, if using an IPv4 address
• ::1 // same as loopback, if using an IPv6 address
• 0:0:0:0:0:0:0:1 // same as loopback, if using an IPv6 address
• 130.123.246.45 //sample IPv4 address
• fe80::fd39:f4ed:1938:97e6%24 // sample IPv6 address

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