Socket Programming Sample Code - simple chat client and server ee122 sp05 daron spektor.

Slides:



Advertisements
Similar presentations
Introduction to Information Security Networking. Transmission Control Protocol (aka TCP) Most widely used protocol A TCP Connection is based on 6 crucial.
Advertisements

Socket Programming Application Programming Interface.
Network Server Programming Expression Evaluation Tutorial #10 CPSC 261.
Quick Overview. 2 ISO/OSI Reference Model Application Application Presentation Presentation Session Session Transport Transport Network Network Data Link.
Windows Sockets Purpose Windows Sockets 2 (Winsock) enables programmers to create advanced internet, intranet, and other network-capable applications to.
Tutorial 8 Socket Programming
EECC694 - Shaaban #1 lec #14 Spring The Application Layer Client/Server Computing, Basic Approaches: –Passing Messages. Example: Communication.
1 Socket Programming A crash-course in network programming…
CS 360 – Spring 2007 Pacific University TCP section 6.5 (Read this section!) 27 Feb 2007.
Introduction to Information Security Networking. Transmission Control Protocol (aka TCP) Most widely used protocol A ‘reliable’ (but not secure!) protocol.
Networking S04, Recitation, Section A
Recitation 12: 11/25/02 Outline Socket Interface –Echo Client/Server Http Protocol Evaluation Annie Luo Office Hours: Thursday.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
Copyright © University of Illinois CS 241 Staff1 Network Programming.
Sockets API Overview Sockets with UDP Sockets with TCP Fast Sockets (Fast UDP) IP Multicasting.
Lab #1: Network Programming using Sockets By J. H. Wang Nov. 28, 2011.
1 Internet Socket programming Behzad Akbari. 2 Sharif University of Technology, Kish Island Campus What is an API? API – stands for Application Programming.
Winsock Programming Blocking and Asynchronous Sockets for Windows.
Recitation 12 (Nov. 29) Outline Socket programming Lab 7: part 1 Reminder Lab 7: Due next Thursday Minglong Shao Office hours: Thursdays.
Ports Port - A 16-bit number that identifies the application process that receives an incoming message. Reserved ports or well-known ports (0 to 1023)
Technical Details for sockaddr_in Structure Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
Lab 5 Sockets. Useful Sockets Links (courtesy of Stanford University) Programming UNIX Sockets in C - Frequently Asked Questions
Computer Networks Lecture 2 Adrian Sergiu DARABANT.
Socket Programming Lec 2 Rishi Kant. Review of Socket programming Decide which type of socket – stream or datagram. Based on type create socket using.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
An Introductory 4.4BSD Interprocess Communication Tutorial Stuart Sechrest.
Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol ,
Introduction to Socket
Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
1 제 12 장 프로세스 사이의 통신. 2 Objectives describe how pipes are used for IPC define the two kinds of pipes use the system calls used with pipes network programming.
Programming with UDP – II Covered Subjects: Creating UDP sockets Client Server Sending data Receiving data Connected mode.
Socket Program Training 10/29/ TCP Client Socket ( ) Connect ( ) send ( ) Close ( ) send ( ) Read ( ) Accept ( ) recv ( ) Listen ( ) Bind ( ) Socket.
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.
Read() recv() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept()
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
In unistd.h : int gethostname(char * name, int maxlen) Purpose : Find the computer's name.
0 BOF 기초부터 응용까지~! Feat. LoB
1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level connection.Establishing.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University.
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.
Socket Program Training 10/27/2010. What is a Socket ? An interface between an application process and transport layer (TCP or UDP). 2.
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.
Netprog: TCP Sockets1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level.
Introduction to Information Security Networking 1.
Sockets and Beginning Network Programming
CS 1652 Jack Lange University of Pittsburgh
Extending echo server HTTP Broken pipe error Feedback and evaluation
Pertemuan 7 I/O Multiplexing
CS 105 “Tour of the Black Holes of Computing!”
Introduction to Information Security
Review: TCP Client-Server Interaction
Imam Ahmad Trinugroho, ST., MMSI
Recitation 11 – 4/29/01 Outline Sockets Interface
תקשורת ומחשוב תרגול 3-5 סוקטים ב-C.
TCP Sockets Programming
Chapter 12 Interprocess Communication
Chapter 06. UDP Server/Client.
Socket Programming(1/2)
Chapter 04. TCP Server/Client.
Socket Programming Neil Tang 09/08/2008
Internet Networking recitation #8
in unistd.h: int gethostname(char * name, int maxlen)
Presentation transcript:

Socket Programming Sample Code - simple chat client and server ee122 sp05 daron spektor

#include int main() { int serverfd, clientfd; struct sockaddr_in myaddress; struct sockaddr_in cliaddress; char buf[200]; myaddress.sin_family = AF_INET; myaddress.sin_port = htons(12205); myaddress.sin_addr.s_addr = htonl(INADDR_ANY); memset(&(myaddress.sin_zero), 0, 8); serverfd = socket(AF_INET, SOCK_STREAM, 0); bind(serverfd, (struct sockaddr *)&myaddress, sizeof(struct sockaddr)); listen(serverfd, 2); while (1) { int addrlen = sizeof(struct sockaddr); int len; clientfd = accept(serverfd, (struct sockaddr *)&cliaddress, &addrlen); printf("[[connected to %s]]\n", inet_ntoa(cliaddress.sin_addr)); while (len = recv(clientfd, buf, 200, 0)) { puts(buf); } printf("connection reset\n"); close(clientfd); } return 0; } TCP one-way chat server

#include int main() { int serverfd; struct sockaddr_in serveraddress; char buf[200]; serveraddress.sin_family = AF_INET; serveraddress.sin_port = htons(12205); serveraddress.sin_addr.s_addr = inet_addr(" "); memset(&(serveraddress.sin_zero), 0, 8); serverfd = socket(AF_INET, SOCK_STREAM, 0); connect(serverfd, (struct sockaddr*)&serveraddress, sizeof(struct sockaddr)); while (fgets(buf, 200, stdin)) { send(serverfd, buf, strlen(buf)+1, 0); } close(serverfd); return 0; } TCP one-way chat client

#include int main() { int serverfd, clientfd, temp; struct sockaddr_in myaddress; struct sockaddr_in cliaddress; char buf[200]; serverfd = socket(AF_INET, SOCK_STREAM, 0); if (serverfd<0) { perror("socket error"); exit(1); } myaddress.sin_family = AF_INET; myaddress.sin_addr.s_addr = htonl(INADDR_ANY); myaddress.sin_port = htons(12205); memset(&(myaddress.sin_zero), 0, 8); temp = bind(serverfd, (struct sockaddr *)&myaddress, sizeof(struct sockaddr)); if (temp<0) { perror("bind error"); exit(1); } if (listen(serverfd, 5)<0) { perror("problem listening"); exit(1); } while (1) { int addrlen = sizeof(struct sockaddr); int len; if ((clientfd = accept(serverfd, (struct sockaddr *)&cliaddress, &addrlen))<0) { perror("accept error"); exit(1); } printf("[[connected to %s]]\n", inet_ntoa(cliaddress.sin_addr)); while (len = recv(clientfd, buf, 200, 0)) { puts(buf); } close(clientfd); if (len==0) { printf("connection reset\n"); } else if (len<0) { perror("recv error"); exit(1); } return 0; } server code with error checking

#include int main() { int serverfd; struct sockaddr_in serveraddress; char buf[200]; if ((serverfd = socket(AF_INET, SOCK_STREAM, 0))<0) { perror("socket error"); exit(1); } serveraddress.sin_family = AF_INET; serveraddress.sin_port = htons(12205); serveraddress.sin_addr.s_addr = inet_addr(" "); memset(&(serveraddress.sin_zero), 0, 8); if ((connect(serverfd, (struct sockaddr*)&serveraddress, sizeof(struct sockaddr)))<0) { perror("connect error"); exit(1); } while (fgets(buf, 200, stdin)) { send(serverfd, buf, strlen(buf)+1, 0); } close(serverfd); return 0; } client code with error checking