1 Socket Programming A crash-course in network programming…

Slides:



Advertisements
Similar presentations
Socket Programming Application Programming Interface.
Advertisements

Windows Sockets Purpose Windows Sockets 2 (Winsock) enables programmers to create advanced internet, intranet, and other network-capable applications to.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
1 Pertemuan 10 Non Blocking Matakuliah: H0483 / Network Programming Tahun: 2005 Versi: 1.0.
Tutorial 8 Socket Programming
Socket Programming Sample Code - simple chat client and server ee122 sp05 daron spektor.
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Socket Addresses. Domains Internet domains –familiar with these Unix domains –for processes communicating on the same hosts –not sure of widespread use.
Data Communications and Networking COMP 2330 Laboratory 5 Mr. Kaiyong Zhao Please sign your name with your attendance.
ISP – 9 th Recitation Socket programming – Client side.
I NTRODUCTION OF S OCKET P ROGRAMMING L.Aseel AlTurki King Saud University.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
CS3212 計算機網路概論 Winsock Tutorial
Client Software Design Objectives: Understand principles of C/S design, with focus on clients Review Windows implementations of Socket functions.
Cs423-cotter1 Example Client Program Reference Comer & Stevens, Chapter 7.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
Socket Programming References: redKlyde ’ s tutorial set Winsock2 for games (gamedev.net)
CS1652 September 13th, 2012 The slides are adapted from the publisher’s material All material copyright J.F Kurose and K.W. Ross, All Rights.
Fall 2000Datacom 11 Socket Programming Review Examples: Client and Server-Diagnostics UDP versus TCP Echo.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
1 Ch 3. Network Programming. 2 Network Programming (1)  Network allows arbitrary applications to communicate E.g., client-server computing such as WEB.
Assignment 3 A Client/Server Application: Chatroom.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
Sockets API Overview Sockets with UDP Sockets with TCP Fast Sockets (Fast UDP) IP Multicasting.
1 Writing Network Applications using the TCP/IP Protocol Stack: Socket Programming.
Remote Shell CS230 Project #4 Assigned : Due date :
Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS A: Windows Networking A.2. Windows Sockets.
The Pocket Guide to TCP/IP Sockets: C Version Michael J. Donahoo Kenneth L. Calvert.
Socket Programming Lec 2 Rishi Kant. Review of Socket programming Decide which type of socket – stream or datagram. Based on type create socket using.
CS 158A1 1.4 Implementing Network Software Phenomenal success of the Internet: – Computer # connected doubled every year since 1981, now approaching 200.
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
Introduction to Socket
Chapter 27 Socket API Interface The interface between an application program and the communication protocols in an operating system is known as the Application.
Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
Socket Programming Lab 1 1CS Computer Networks.
2: Application Layer 1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r Internet gaming r 2.3 FTP r 2.4 Electronic.
Socket Programming.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
CS3212 計算機網路概論 Winsock Tutorial. Dev C++ environment setting Download Dev-C++ from
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
Network Programming By J. H. Wang Nov. 21, Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Berkeley Socket Abstraction
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
1 Socket Interface. 2 Client-Server Architecture The client is the one who speaks first Typical client-server situations  Client and server on the same.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
1 Vehicle Networking Networks Instruction 1 – Echo client/server in C Jeroen Voeten ES, 2012.
Sockets and Beginning Network Programming
Assignment 3 A Client/Server Application: Chatroom
Jim Fawcett CSE 681 – Software Modeling & Analysis Fall 2002
CS 1652 Jack Lange University of Pittsburgh
Jim Fawcett CSE 687-OnLine – Object Oriented Design Summer 2017
The Pocket Guide to TCP/IP Sockets: C Version
Jim Fawcett CSE 775 – Distributed Objects Spring 2007
Socket Programming in C
28.
Windows Sockets Šarūnas Valiukas.
Chapter 06. UDP Server/Client.
Chapter 3 Socket API © Bobby Hoggard, Department of Computer Science, East Carolina University These slides may not be used or duplicated without permission.
Socket Programming(1/2)
Chapter 04. TCP Server/Client.
Socket Programming Neil Tang 09/08/2008
Internet Networking recitation #8
Jim Fawcett CSE 681 – Software Modeling & Analysis Summer 2003
Presentation transcript:

1 Socket Programming A crash-course in network programming…

© HC/RF 5JJ70 pg 2 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 3 Client/Server communication So…how 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 4 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 don’t 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 5 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 6 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 7 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 8 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 9 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 10 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 11 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 12 More on winsock Echo Server/Client code available at the Computation website: – –download echoc.cpp and echod.cpp Some WinSock tutorials on the Internet: – – – – #theoryhttp://beej.us/guide/bgnet/output/html/singlepage/bgnet.html #theory – ial_1.shtm