Programming - TU/e

Programming - TU/e

Socket Programming A crash-course in network programming 1 Socket basics Network : a collection of computers connected to each other so they can exchange data. Networks rely on a layered set of protocols (e.g. TCP/IP, UDP), which provide services (like reliability, data consistency, congestion control) to the programmer. WinSock provides an interface to these network protocols Windows Sockets, derived from the BSD Sockets API A socket is an endpoint of a bi-directional communication flow over an IP-based network (like Internet). Characterized by a protocol type, remote socket address and local socket address. Socket addresses consist of an IP-address and a Port Number.

IP-address maps a number to a specific computer Port number maps a number to a specific process/application running on a computer HC/RF 5JJ70 pg 2 Client/Server communication Sohow can two computers communicate over a network? Client/Server model Server: program/computer providing a service Creates a local socket Binds local socket to a specific port Listens for incoming connections Accepts a connection, assigning a new socket for the connection Sends/receives data Client: program/computer requesting a service Determines remote socket address Creates a local socket

Connects to remote socket Sends/receives data HC/RF 5JJ70 pg 3 Setting up the WinSock API Include the winsock2.h header file: #include Link with WinSock library: type ws2_32.lib in project->properties->linker->commandline>additional options Initizalize Winsocket API: Declare a WSADATA variable to store info on WinSock (you dont need to set it, only declaration is enough) WSADATA info; Call to WSAStartup function: if (WSAStartup(MAKEWORD(1,1), &info) !=0){ printf("Winsock startup failed\n");

exit(1); } HC/RF 5JJ70 pg 4 Setting up a server (1) Create local socket: Declare a variables of type sockaddr and type SOCKET: struct sockaddr_in server_address; struct sockaddr_in client_address; SOCKET s, new_s; Set up the local socket: #define SERVER_PORT 5432 //port number of server > 1024 server_address.sin_family = AF_INET; server_address.sin_addr.s_addr = INADDR_ANY; server_address.sin_port = htons(SERVER_PORT); memset(&(server_address.sin_zero),'\0', 8);

Create a new socket s = socket(PF_INET, SOCK_STREAM, 0); if (s == INVALID_SOCKET) { printf("Socket creation failed\n"); exit(1); } HC/RF 5JJ70 pg 5 Setting up a server (2) Bind the socket (associate with a specific port number): if (bind(s, (struct sockaddr *)&server_address, sizeof(server_address)) == SOCKET_ERROR) { printf("Bind failed\n"); exit(1); }

Listen for incoming connections on the new socket: #define MAX_PENDING 2 //max # simultaneous connections listen(s, MAX_PENDING); Accept incoming connection; connects to a new socket: len = sizeof(client_address); new_s = accept(s,(struct sockaddr *)&client_address,&len); if (new_s == INVALID_SOCKET) { printf("Accepting connection failed\n"); exit(1); } HC/RF 5JJ70 pg 6 Setting up a server (3) Now we can send and receive: send (SOCKET s, const char FAR * buf, int len, int flags); recv (SOCKET s, char FAR * buf, int len, int flags);

Example: receive a string, and then send back an echo: char buf[50]; n=recv(new_s, buf, sizeof(buf), 0); buf[n]='\0'; send(new_s, buf, n, 0); At the end of the program, free socket resources: closesocket(s); WSACleanup(); HC/RF 5JJ70 pg 7 Setting up a client (1) Initialize WinSock API, like we did with the server: Include header, link with library Start up WinSock: WSADATA info; if (WSAStartup(MAKEWORD(1,1), &info) !=0){

printf("Winsock startup failed\n"); exit(1); } Lookup remote server address: Declare variable of type struct hostent: struct hostent *hp; Get server address and store it in variable: char *servername; hp = gethostbyname(servername); if (!hp) { printf("Unknown server: %s\n",servername); exit(1); } HC/RF 5JJ70 pg 8

Setting up a client (2) Set up socket: SOCKET s; struct sockaddr_in server_address; server_address.sin_family = AF_INET; server_address.sin_addr = *((struct in_addr *)hp>h_addr); server_address.sin_port = htons(SERVER_PORT); memset(&(server_address.sin_zero),'\0',8); Create socket: s = socket(PF_INET, SOCK_STREAM, 0); if (s == INVALID_SOCKET){ printf("Socket creation failed"); exit(1); } HC/RF 5JJ70 pg 9

Setting up a client (3) Connect to remote socket of server: if (connect(s,(struct sockaddr *)&server_address, sizeof(server_address)) == SOCKET_ERROR) { printf("Connecting to server failed"); closesocket(s); exit(1); } Now we can send and receive: send (SOCKET s, const char FAR * buf, int len, int flags); recv (SOCKET s, char FAR * buf, int len, int flags); Example: sending a string and receiving an echo from server: char buf[256], buf_in[256]; n = strlen(buf); send(s,buf,n,0); n=recv(s, buf_in, sizeof(buf_in), 0); buf_in[n] = '\0';

printf("Echo generated by server %s:\n", inet_ntoa(server_address.sin_addr)); printf(buf_in); HC/RF 5JJ70 pg 10 The Echo server/client in action Client sends strings to Server on same pc, on local loopback address Server on same pc over Internet on external IP-address Server on other PC (also running echoserver) over Internet on external IP-address Some wrong IP-address HC/RF 5JJ70 pg 11

More on winsock Echo Server/Client code available at the Computation website: http://www.es.ele.tue.nl/~heco/courses/Computation/ index.html download echoc.cpp and echod.cpp Some WinSock tutorials on the Internet: http://tangentsoft.net/wskfaq/ http://www.linuxhowtos.org/C_C++/socket.htm http://www.madwizard.org/programming/tutorials/netcpp/3 http://beej.us/guide/bgnet/output/html/singlepage/ bgnet.html#theory http://www.win32developer.com/tutorial/winsock/ winsock_tutorial_1.shtm HC/RF 5JJ70 pg 12

Recently Viewed Presentations

  • PowerPoint Presentation

    PowerPoint Presentation

    Elijah's reassurance . So she went away and did according to the word of Elijah; and she and he and her household ate for many days. 16 The bin of flour was not used up, nor did the jar of...
  • WELCOME TO Why Sell Spectrum Bundles? Convenience and

    WELCOME TO Why Sell Spectrum Bundles? Convenience and

    To get started, direct customers to your ACN personal website. They will click on Services, and then under Residential Services, click on Bundles. ... Manager your home phone from anywhere, at home or on-the-go with Voice Online Manager. FREE.
  • Factoring Polynomials - Cengage

    Factoring Polynomials - Cengage

    A polynomial is factored completely when it is written as a product of factors that can not be factored further. = 4x(x2) - 4x(10x) + 4x(25) The GCF is 4x. = 4x(x2 - 10x + 25) Use distributive property to...
  • Evaluation for OCR coursework Media Studies This is

    Evaluation for OCR coursework Media Studies This is

    Media Studies. In what ways does your production use, develop or challenge forms and conventions of real fiction films in your chosen genre? To answer this you need to. be quite sure of your genre. take specific examples from yourfilm...
  • Principles to Produce Nutrient Dense Crops

    Principles to Produce Nutrient Dense Crops

    Crops endure stress easily More uniform growth Crops mature sooner Greater plant vitality Greater fruiting Increased yield Increased density Greater seed formation Stronger seed viability More, better flavor Longer shelf life Less spoilage & rot the energy flowing through them...
  • Alternative ways of measuring neighbourhood ethnic density Mai

    Alternative ways of measuring neighbourhood ethnic density Mai

    Alternative ways of measuring neighbourhood ethnic density Mai Stafford & Laia Becares Dept Epidemiology & Public Health, UCL James Nazroo CCSR & School of Social Sciences, University of Manchester
  • GRAINS: GOOD FOR YOU - Cabarrus County Schools

    GRAINS: GOOD FOR YOU - Cabarrus County Schools

    BROWN RICE: whole kernel with just the hard hull covering removed. WHITE RICE: the hull, bran, and germ have been removed. CONVERTED RICE: this rice was briefly boiled with the hull on, then hulled and dried to preserve the nutrients....
  • PowerPoint Presentation

    PowerPoint Presentation

    California Youth Energy Services (CYES) program, in operation since 2000, is a highly innovative youth employment and empowerment program, addressing climate change and providing valuable no-cost services to thousands of local residents each year.