Chapter 02. Starting Windows Socket. IT COOKBOOK - 1 -  Goal Error handling routine for Winsock function error Winsock startup and cleanup Socket creation.

Slides:



Advertisements
Similar presentations
Network Programming Week #1 J.P. Yoo
Advertisements

Computer Net Lab/Praktikum Datenverarbeitung 2 1 Overview Sockets Sockets in C Sockets in Delphi.
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
Networked Graphics Building Networked Virtual Environments and Networked Games Distributed Objects, Exchange of Information, Socket Programming.
Win32 Programming Lesson 5: Error Codes. Before We Begin  Much of the time in this class we’ll be calling Win32 Functions  However, sometimes they’re.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
1 JMH Associates © 2000, All rights reserved Chapter 12 Windows Sockets and Network Programming.
Sockets Basics Conectionless Protocol. Today IPC Sockets Basic functions Handed code Q & A.
Windows Sockets Purpose Windows Sockets 2 (Winsock) enables programmers to create advanced internet, intranet, and other network-capable applications to.
1 Pertemuan 10 Non Blocking Matakuliah: H0483 / Network Programming Tahun: 2005 Versi: 1.0.
Sockets Programming Introduction © Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid.
#include DatatypeDescription int8_t uint8_t int16_t uint16_t int32_t uint32_t Signed 8-bit integer Unsigned 8-bit integer Signed 16-bit integer Unsigned.
Socket Addresses. Domains Internet domains –familiar with these Unix domains –for processes communicating on the same hosts –not sure of widespread use.
Client Server Model The client machine (or the client process) makes the request for some resource or service, and the server machine (the server process)
Winsock programming.  TCP/IP UDP TCP  Winsock #include wsock32.lib.
ISP – 9 th Recitation Socket programming – Client side.
1 Socket Programming A crash-course in network programming…
Advanced Programming in the UNIX Environment Hop Lee.
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.
Network Programming Sockets and Winsock. Please Be Responsible We all know that the Internet is full of security holes –most of them do not require any.
ECE453 – Introduction to Computer Networks Lecture 15 – Transport Layer (II)
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
Socket Programming References: redKlyde ’ s tutorial set Winsock2 for games (gamedev.net)
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Assignment 3 A Client/Server Application: Chatroom.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
Sirak Kaewjamnong Computer Network Systems
The Application Layer Application Services (Telnet, FTP, , WWW) Reliable Stream Transport (TCP) Connectionless Packet Delivery Service (IP) Unreliable.
File IO and command line input CSE 2451 Rong Shi.
Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS A: Windows Networking A.2. Windows Sockets.
1 Example Servers Pt 1 Objective: To discuss key aspects of various server implementations.
Outline Socket programming with Windows OSSocket programming with Windows OS C++ UDPSocket classC++ UDPSocket class Socket programming with Windows OSSocket.
An Introductory 4.4BSD Interprocess Communication Tutorial Stuart Sechrest.
Elementary TCP Sockets UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
CSE/EE 461 Getting Started with Networking. 2 Basic Concepts A PROCESS is an executing program somewhere. –Eg, “./a.out” A MESSAGE contains information.
Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
Practical Sockets and Threads Derek Weitzel. Windows Threads Concurrent processing Concurrent processing Windows Create Thread Windows Create Thread HANDLE.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Intro to Socket Programming CS 360. Page 2 CS 360, WSU Vancouver Two views: Server vs. Client Servers LISTEN for a connection and respond when one is.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
UNIX Sockets Outline UNIX sockets CS 640.
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Socket programming in C. Socket programming with TCP Client must contact server server process must first be running server must have created socket (door)
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.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
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.
SOCKET PROGRAMMING Presented By : Divya Sharma.
Assignment 3 A Client/Server Application: Chatroom
Socket programming Péter Verhás August 2002
Jim Fawcett CSE 681 – Software Modeling & Analysis Fall 2002
Part 2 Socket Programming UDP.
Network and Application Packet Analyzer
Internet Address Routines
Jim Fawcett CSE 687-OnLine – Object Oriented Design Summer 2017
Jim Fawcett CSE 775 – Distributed Objects Spring 2007
Transport layer API: Socket Programming
Command Line Arguments
28.
30.
27.
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.
Windows APIs Some odds and ends Copyright © 1997 – 2016 Curt Hill.
Socket Programming(1/2)
Jim Fawcett CSE 681 – Software Modeling & Analysis Summer 2003
Presentation transcript:

Chapter 02. Starting Windows Socket

IT COOKBOOK  Goal Error handling routine for Winsock function error Winsock startup and cleanup Socket creation and close

IT COOKBOOK  Winsock error handling Getting error code Example int WSAGetLastError (void) ; if (socketfunction(...) == error) { int errcode = WSAGetLastError(); printf(error message for errcode); }

IT COOKBOOK  Conversion error code to string (1/4) FormatMessage() function - convert error code to error message DWORD FormatMessage ( DWORD dwFlags, // ① option LPCVOID lpSource, // NULL DWORD dwMessageId, // ② error code DWORD dwLanguageId, // ③ language LPTSTR lpBuffer, // ④ starting address of error string DWORD nSize, // 0 va_list* Arguments // NULL ) ; success: length of error message, fail: 0

IT COOKBOOK  Conversion error code to string(2/4) err_quit() function definition - Error message print and exit #include void err_quit(char *msg) { LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER| FORMAT_MESSAGE_FROM_SYSTEM, NULL, WSAGetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL); MessageBox(NULL, (LPCTSTR)lpMsgBuf, msg, MB_ICONERROR); LocalFree(lpMsgBuf); exit(-1); }

IT COOKBOOK  Conversion error code to string(3/4) Example of err_quit() function Error display if (socket(...) == SOCKET_ERROR) err_quit("socket()"); if (bind(...) == SOCKET_ERROR) err_quit("bind()"); String in err_quit() Error message for error code

IT COOKBOOK  Conversion error code to string(4/4) err_display() function definition #include void err_display(char *msg) { LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER| FORMAT_MESSAGE_FROM_SYSTEM, NULL, WSAGetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL); printf("[%s] %s", msg, (LPCTSTR)lpMsgBuf); LocalFree(lpMsgBuf); }

IT COOKBOOK  Winsock startup and cleanup (1/3) Winsock startup call –wVersionRequested The highest version of Windows Sockets specification that the caller can use. The high-order byte specifies the minor version number; the low-order byte specifies the major version number. –lpWSAData A pointer to the WSADATA data structure int WSAStartup ( WORD wVersionRequested, LPWSADATA lpWSAData ) ; success: 0, fail: error code

IT COOKBOOK  Winsock startup and cleanup(2/3) Winsock cleanup call int WSACleanup (void) ; 성공 : 0, 실패 : SOCKET_ERROR

IT COOKBOOK  Winsock startup and cleanup(3/3) Code example (InitWinsock.cpp): #include int main(int argc, char* argv[]) { // Winsock startup WSADATA wsa; if(WSAStartup(MAKEWORD(2,2), &wsa) != 0) return -1; MessageBox(NULL, " Winsock startup ", “ success", MB_OK); // Winsock cleanup WSACleanup(); return 0; }

IT COOKBOOK  socket creation and close (1/6) Socket creation call –Returning an integer value result, similar to a file desriptor, that can be used in subsequent socket system calls to refer to the socket SOCKET socket ( int af, // Family int type, // Socket type int protocol // Protocol ) ; success: new socket, fail: INVALID_SOCKET

IT COOKBOOK  socket creation and close(2/6) Family #define AF_UNIX 1 /* local to host (pipes, portals) */ #define AF_INET 2 /* internetwork: UDP, TCP, etc. */ #define AF_IMPLINK 3 /* arpanet imp addresses */ #define AF_PUP 4 /* pup protocols: e.g. BSP */ #define AF_CHAOS 5 /* mit CHAOS protocols */ #define AF_NS 6 /* XEROX NS protocols */ #define AF_IPX AF_NS /* IPX protocols: IPX, SPX, etc. */ #define AF_ISO 7 /* ISO protocols */ #define AF_OSI AF_ISO /* OSI is ISO */...

IT COOKBOOK  socket creation and closing (3/6) Socket type –Protocol to be used example) TCP 와 UDP protocol(1) Socket typeproperty SOCK_STREA M Providing reliable data transfer Connection-oriented protocol SOCK_DGRAMProviding unreliable data transfer Connectionless protocol protocolfamilySocket type TCP AF_INET SOCK_STREAM UDPSOCK_DGRAM

IT COOKBOOK  socket creation and close(4/6) Protocol familySocket typeProtocol TCP AF_INET SOCK_STREAMIPPROTO_TCP or 0 UDPSOCK_DGRAMIPPROTO_UDP or 0

IT COOKBOOK  socket creation and close(5/6) Socket closing call –Closing socket and returning the related resource int closesocket ( SOCKET s ) ; success: 0, fail: SOCKET_ERROR

IT COOKBOOK  socket creation and close(6/6) Code example int main(int argc, char* argv[]) { // winsock startup... // socket() SOCKET tcp_sock = socket(AF_INET, SOCK_STREAM, 0); if(tcp_sock == INVALID_SOCKET) err_quit("socket()"); MessageBox(NULL, "TCP socket creation success", “ success", MB_OK); // closesocket() closesocket(tcp_sock); // winsock cleanup... }