Win Sock

  • Uploaded by: om18sahu
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Win Sock as PDF for free.

More details

  • Words: 714
  • Pages: 17
Winsock is short for Windows Sockets, it is a specification that defines how Windows network applications should access network services.  Winsock is based on BSD Sockets, but it provides additional functionality to allow the API to comply with the standard Windows programming model 

Windows extensions to socket functions:  – WSAAsyncSelect,  – WSAAsyncGetHostByAddr,  – WSAAsyncGetHostByName 

Windows implements BSD sockets:  – socket – create socket  – connect – connect client to server  – bind – bind to specific address  – send – send data to the other side of the socket  – recv – receive data sent on a socket  – listen – set up listening socket (server)  – accept – accept incoming connection (server) 



A socket is very much like a telephone - it's the endpoint of a two-way communications channel. By connecting two sockets together you can pass data between processes, even processes running on different computers, just as you can talk over the telephone once you've made a phone call connecting your telephone with someone else's.

• Before using sockets Windows application must call WSAStartup • Before shutdown, Windows application should call WSACleanup • WSAStartup requests specific version of Windows Socket implementation. This function can fail if Windows does not support particular version of Windows Sockets.

#include <windows.h> #include <winsock.h> #include <stdio.h> #include <string.h> #pragma comment(lib, "ws2_32.lib") int main(int argc, char** argv) { WSADATA WSAData; WSAStartup(0x202, &WSAData); return 0; }

• call WSAStartup • create socket with socket function • set socket to blocking mode with ioctlsocket (in UNIX ioctl) • connect to the server with connect function • send or receive some data to/from server with send and recv • close the socket with closesocket (in UNIX close) • call WSACleanup

• • • •

call WSAStartup create socket with socket function bind socket to a port with bind function start listening for requests with listen function • call select function in a loop to find out if connection request on listening socket is pending • if there is connect request, call accept to accept connection request. • accept creates new socket used for communication with a client

• communicate with the client using send and recv functions • close the client socket with closesocket • (optional) accept new requests from clients • close listening socket with closesocket • call WSACleanup

• Socket can operate in one of two modes: – blocking – non blocking • In blocking mode functions like connect, recv, send wait for the operation to complete • In non blocking mode those functions return immediately and: – connect starts background connect operation – send, recv send or receive only the amount of data that will not block. The operation should be repeated by the application

Socket mode can be set using ioctlsocket function, for example: ioctlsocket( so, FIONBIO, 1 ); // set nonblocking ioctlsocket( so, FIONBIO, 0 ); // set blocking mode

Servers that must communicate with many clients at the same time, must do one of the following: – use blocking sockets and start one thread for each client (each socket) – use non blocking sockets and run select in a loop to find out which sockets are available for reading/writing – use non blocking sockets and use Windows extension to sockets (WSAAsyncSelect) • The same applies to clients using many connections at the same time

Struct sockaddr_in addr; SOCKET acceptSocket; acceptSocket = socket( AF_INET, SOCK_STREAM, 0 ); if ( acceptSocket == INVALID_SOCKET ) { return false; } memset( &addr, 0, sizeof( addr )); addr.sin_family = AF_INET; addr.sin_port = htons( portNumber ); addr.sin_addr.S_un.S_addr = 0; if ( bind( acceptSocket, (struct sockaddr *)&addr, 

sizeof( addr )) != 0 ) { return false; } if ( listen( acceptSocket, 5 ) != 0 ) {return false; }

WSAAsyncSelect converts sockets events to Windows messages int WSAAsyncSelect( SOCKET s, // socket HWND hWnd, // window that will receive messages unsigned int wMsg, // message number long lEvent // events that should be sent to the window ); • lEvent is a combination of flags: – FD_READ, FD_WRITE, FD_ACCEPT, – FD_CONNECT, FD_CLOSE, FD_OOB – FD_QOS, FD_GROUP_QOS, – FD_ROUTING_INTERFACE_CHANGE, FD_ADDRESS_LIST_CHANGE 

Windows extensions: functions with WSA prefix • WSAAsyncGetHostByName • WSAGetLastError • WSAStartup • WSACleanup 

Thanks

Related Documents

Win Sock
May 2020 7
Win Sock
May 2020 3
Sock Innovation
November 2019 9
Sock Chien
October 2019 12
Sock Zine1
June 2020 3
Win
December 2019 48

More Documents from "julymoe"

Xns Protocol
May 2020 11
Chapter 3: Sql
June 2020 7
Java Be An
May 2020 8
Dynamic Link Library
May 2020 10
Lecture 1
June 2020 8
June 2020 2