Introduction to Winsock Network Programming YunYang Ma 20080729
- Slides: 26
Introduction to Winsock Network Programming Yun-Yang Ma 2008/07/29
Outline • • • Introduction Example Winsock API Source Code Reference
Introduction • Internet socket – An end-point of bidirectional process-toprocess communication flow across IP based network. – Each socket is mapped to an application process or thread. socket
Introduction • A socket is a unique combination of the following – Protocol (TCP, UDP, or raw IP) – Local IP address – Local port number Local Program – Remote IP address port number – Remote port number Remote Program protocol IP address port number
Introduction • An socket can be opened on any port
Introduction • Winsock – Windows Socket – Framework Winsock應用程式層, 如 Netscape, Cute Ftp … 等 底層傳輸核心, 如 TCP/IP 核心 網路卡驅動程式 網路實體 – Client-Server Model Winsock API
Example Echo Program – Echo server – Echo client
Example • Execute echo server program
Example • Execute echo client program
Example • Enter a string and get echo message
Example • Received message at server
Example • Terminate the connection
Winsock API • • SOCKET socket ( int af, int type, int protocol) – The socket function creates a socket – af : The address family specification. (AF_INET : IPv 4) – type : The type specification of a new socket. – protocol : The protocol to be used. Int bind ( SOCKET s, const struct sockaddr *name, int namelen) – The bind function associates a local address with a socket. – s : Descriptor identifying a socket. – name : Address to assign to the socket from the sockaddr structure. – namelen : Length of the value in the name parameter.
Winsock API • • int listen ( SOCKET s, int backlog) – The listen function places a socket a state where it is listening for an incoming connection. – backlog : The maximum length of the queue of pending connections. SOCKET accept ( SOCKET s, struct sockaddr *addr, int *addrlen) – The accept function permits an incoming connection attempt on a socket. – addr : An optional pointer to a buffer that receives the address of the connecting entity. – addrlen : An optional pointer to an integer that contains the length of structure pointed to by the addr parameter.
Winsock API • • • Int connect ( SOCKET s, const struct sockaddr *name, int namelen) – The connect function establishes a connection to a specified socket. – name : Name of the socket in the sockaddr structure to which the connection should be established. Int closesocket ( SOCKET s ) – The closesocket function closes an existing socket. Int send ( SOCKET s, const char *buf, int len, int flags) – The send function sends data on a connected socket. – buf : A pointer to a buffer containing the data to be transmitted. – len : The length of the data in buffer pointed to by the buf parameter. – flags : A set of flags that specify the way in which the call is made.
Winsock API • • • Int recv ( SOCKET s, char *buf, int len, int flags) – The recv function receives data from a connected socket. – buf : A pointer to the buffer to receive the incoming data. Int WSAStartup ( WORD w. Version. Requested, LPWSADATA lp. WSAData) – The WSAStartup function initiates use of the Winsock DLL by a process. – w. Version. Requested : The highest version of Windows Sockets specification that the caller can use. – lp. WSAData : A pointer to the WSADATA data structure that is to receive details of the Windows Sockets implementation. WSACleanup ( void ) – The WSACleanup function terminates use of the Winsock DLL.
Winsock API • • Inet_addr ( const char *cp) – The inet_addr function converts a string containing an IPv 4 dotteddecimal address into a proper address for the IN_ADDR structure. – cp : A NULL-terminated character string representing a number expressed in the Internet standard ". '' (dotted) notation. htons ( u_short hostshort) – The htons function converts a u_short from host to TCP/IP network byte order (which is big-endian). – hostshort : 16 -bit number in host byte order.
Source Code • Echo server • Header files
Source Code • Main function
Source Code • Main function
Source Code • Main function
Source Code • Main function
Source Code • Echo client • Header files and Main function
Source Code • Main function
Source Code • Main function
Reference • MSDN – http: //msdn. microsoft. com/en-us/library/ms 738557(VS. 85). aspx • 網路程式設計之鑰 (Key to Winsock Network Programming) • Programming Windows TCP Sockets in C++ for the Beginner – http: //www. codeproject. com/KB/IP/beginningtcp_cpp. aspx
- Winsock accept
- Microsoft winsock control
- Recv winsock
- Windows winsock
- Perbedaan linear programming dan integer programming
- Greedy vs dynamic
- Runtime programming
- Linear vs integer programming
- Definisi linear
- Tcp echo client server
- Value result arguments
- Android network programming
- Network model linear programming
- Raw socket in networking
- Ipc in network programming
- Nctu network programming github
- File and record locking in network programming
- Stevens unix
- Introduction to server side programming
- Problem solving
- Introduction to programming languages
- Elementary programming in java
- An introduction to parallel programming peter pacheco
- Introduction to visual basic programming
- Introduction to programming
- Programming and problem solving with java
- Introduction to windows programming