Ά η η 1 (30%): Sockets. socket () bind () listen () accept () connect () read () write () close ()

Similar documents
CS4514 B08 HELP Session 1

CS4700/CS5700 Fundamentals of Computer Networking

ECE322 Systems Programming Project 2: Networking with Matrix Multiplication in C Grant Kimes 12/16/15

Chapter 2 Applications and

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

1 /* client.c - adapted from code for example client program that uses TCP */ 2 /*Modified by Vincent Chu, Winter

Client software design

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

The User Datagram Protocol

l27 handout.txt buggy server.c Printed by Michael Walfish Apr 29, 10 13:41 Page 1/1 Apr 29, 10 11:51 Page 1/1

Programming Requirements. Project & Programming Overview. Local Repository SVN 1/10/2011. Version Control: SVN Makefiles Development Language: C only

Chapter 6. The Transport Layer. Transport Layer 3-1

Piotr Mielecki Ph. D.

System Programming. Sockets: examples

Lab 0. Yvan Petillot. Networks - Lab 0 1

Message passing systems are popular because they support client-server interactions, where: clients send messages to servers requesting a server.

CompSci 356: Computer Network Architectures Lecture 3: Hardware and physical links References: Chap 1.4, 1.5 of [PD] Xiaowei Yang

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Application Programming Interfaces

l27 handout.txt buggy server.c Printed by Michael Walfish Apr 28, 11 15:24 Page 1/1 Apr 27, 11 1:53 Page 1/2

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Cracking WEP Keys Using WEPCrack

Network Programming Worksheet 2. Simple TCP Clients and Servers on *nix with C.

PA #2 Reviews. set_name, get_name, del_name. Questions? Will be modified after PA #4 ~

Lecture 7. Followup. Review. Communication Interface. Socket Communication. Client-Server Model. Socket Programming January 28, 2005

CS307 Operating Systems Processes

Processes. Process Concept. The Process. The Process (Cont.) Process Control Block (PCB) Process State

Sockets Sockets Communication domains

Introduction to Client-Server Model

Azblink API for Sending XMPP Messages via HTTP POST

NETWORK AND SYSTEM PROGRAMMING. I/O Multiplexing: select and poll function

Sockets. 1 Introduction. (Reference:, Gray Chapter 10) Network Programming Lecture Notes by. Turhan TUNALI

UDP CONNECT TO A SERVER

Processes communicating. Network Communication. Sockets. Addressing processes 4/15/2013

CompSci 356: Computer Network Architectures. Lecture 3: Network Architecture Examples and Lab 1. Xiaowei Yang

CS 3516: Computer Networks

ECE 435 Network Engineering Lecture 2

How do we Communicate? Introduction to Unix Network Programming. What does Alice do? What does Bob do? Two simplest networking programs

ICT 6544 Distributed Systems Lecture 5

How to write a Measurement Telnet Server

Programming with TCP/IP. Ram Dantu

CS4514 (C04) HELP Session 1 Introduction to Network Programming (v1.3)

INTEGRATED INFORMATION AND COMMUNICATION LEARNING MODEL FOR RASPBERRY Pi ENVIRONMENT

CSCI 415 Computer Networks Homework 2 Due 02/13/08

ECE 435 Network Engineering Lecture 2

The Transport Layer. The need for the transport layer

Linux Network Programming, Part 1

Network Communication


A. Basic Function Calls for Network Communications

Client/Server. Networking Approach.

Sockets. UNIX-style IPC. Silberschatz, Galvin and Gagne 2005 Msc. Ivan A. Escobar Broitman 2007

Department of Computer Science

The BSD UNIX Socket Interface (CS 640 Lecture) Assignment 1. Interprocess Communication (IPC) Work Individually (no groups)

CS118 Discussion 1B, Week 1. Taqi Raza BUNCHE 1209B, Fridays 12:00pm to 1:50pm

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

Introduction to Berkeley Sockets

MSc Integrated Electronics Networks Assignment. Investigation of TCP/IP Sockets and Ports. Gavin Cameron

// Embedded Systems // BeagleBoard-XM. // Author : Jose Goncalves //

Elementary TCP Sockets

// socket for establishing connections

C Structures in Practice

Project 3. Reliable Data Transfer over UDP. NTU CSIE Computer Networks 2011 Spring

Interprocess Communication Mechanisms

Interprocess Communication

Interprocess Communication Mechanisms

Introduction to Berkeley Sockets

Distributed programming

Client-server model The course that gives CMU its Zip! Network programming Nov 27, Using ports to identify services.

Introduction to Berkeley Sockets

Socket Programming 2007/03/28

CS 640: Computer Networking

TCP Echo Application: Client & Server. TA: Awad A Younis Class: CS457 Fall 2014

CS321: Computer Networks Socket Programming

CSC Systems Programming Fall Lecture - XV Network Programming - I. Tevfik Ko!ar. Louisiana State University. November 9 th, 2010


Unix Network Programming

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

CSE 333 SECTION 8. Sockets, Network Programming

CSE 421/521 - Operating Systems Fall 2011 Recitations. Recitation - III Networking & Concurrent Programming Prof. Tevfik Kosar. Presented by...

How to write a Measurement Telnet Server

Socket Programming. CSIS0234A Computer and Communication Networks. Socket Programming in C

INF 2005 Programmation orientée objet avec C++

Signal Example 1. Signal Example 2

Concurrent Servers. Overview. In our current assignment we have the following changes:

2.2 IoT Internet of Things Android Studio

int write (int fd, void *buf, int nbytes); write will write up to nbytes bytes of data at buf to le descriptor fd. It returns the number of bytes actu

Network Programming in C. Networked Systems 3 Laboratory Sessions and Problem Sets

chat.h #ifndef _CHAT_H #define _CHAT_H #define LBUFFSIZE 128 #define CBUFFSIZE 331

CSE 333 SECTION 7. Client-Side Network Programming

Dept. of Computer Science & Engineering 1 Knowledge & Data Engineering Lab.

NETWORK PROGRAMMING. Instructor: Junaid Tariq, Lecturer, Department of Computer Science

Interprocess Communication. Interprocess Communication

CSE 333 SECTION 7. C++ Virtual Functions and Client-Side Network Programming

Internet protocol stack. Internetworking II: Network programming. April 20, UDP vs TCP. Berkeley Sockets Interface.

Internetworking II: Network programming. April 20, 2000

Unix Network Programming Chapter 4. Elementary TCP Sockets 광운대학교컴퓨터과학과 정보통신연구실 석사과정안중현

Practical Exercises in Computer Networks

#1 socket_server.c socket_client.c

Transcription:

ΗΜΥ 316 - Ε α η ια ή Ά η η 5 Υ ο οίη η Π ω ο ό ο α η αι α α ο ή sockets Ά η η 1 (30%): Sockets π α α α α α π πα π α α ω sockets Unix/Linux. Γ α α α π π α π α Server α Client π π π α έ Α π, α α απ π ω. socket () bind () listen () accept () connect () read () write () close () Ά η η 2 (70%): Π ω ό ο ο αύ η αι α α ο ή π α α α π π ω πα α α α (Stop and wait protocol) α π α α C π α sockets. Θα α α π α απ α (Transmitter) α πα α π (Receiver) π α π ω ω α π α α ( α α α α α α )έ α, απ α α α α α α απ π α α απ ( α α α α ) πα α π α α έ πα α π α π ω α α (π π α απ π π π α α ) απ α (Socket π Client non blocking mode) α α α (channel) port 4610 (server port = 4610) α α α α πα α π (Socket π Server - non blocking mode) port 4613 (Receiver port = 4613). απ α α α α α α ω απ π α α απ α, α α α α έ π α απ α α β α α ω έ π α α α α α sequence number (0 1) α α α αέ Γ α πα α α α α απ π α α format pkt0=0,ch or pkt1=1,ch π ch α α α α έ

α π α α απ frame α ω α ( α α π CRC). Η α π π α π α 1,β π αέ πα α π α π α π ω α π απ απ α α α απα α Acknowledgment (ACK). α α ακ Ο ώ ι α αι η α αφο ά ι α π Email:akyria09@ucy.ac.cy charalampos.menelaou@ucy.ac.cy π, 25 Φ α βί1ζ π αέ κ α(subject) email π π α π ECEγ1ζ_βί1ζ έ α α α π π α π α α έzip α α Assignment5_name_surname έ

Server #include <sys/types.h> #include <sys/time.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <unistd.h> #include <errno.h> #include <signal.h> #include <stdio.h> static void serve(int); #define PORT 6060 main( int argc, char **argv ) int lsd; /* Listening socket */ struct sockaddr_in sin; /* Binding struct */ int sin_size=sizeof(sin); int sd; /* Socket to accept new connexion */ /* Create listening socket */ if ( (lsd=socket(af_inet, SOCK_STREAM, 0)) < 0 ) fprintf(stderr, "%s: cannot create listening socket: ", argv[0]); sin.sin_family = AF_INET; sin.sin_port = htons(port); sin.sin_addr.s_addr = htonl(inaddr_any); if ( bind(lsd, &sin, sin_size) < 0 ) fprintf(stderr, "%s: cannot bind listening socket: ", argv[0]); /* Initiate a listen queue */ if ( listen(lsd, 5) < 0 ) fprintf(stderr, "%s: cannot listen on socket: ", argv[0]);

/* Take care of the SIGPIPE signal - ignore it */ signal(sigpipe, SIG_IGN); while ( 1 ) if ( (sd=accept(lsd, &sin, &sin_size)) < 0 ) exit(errno); serve(sd); shutdown(sd, 2); close(sd); void serve(int sd) time_t local_time; char *time_string; time(&local_time); time_string = ctime(&local_time); write(sd, time_string, strlen(time_string)); return;

Client #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #define BUFSIZE 1024 #define SERVER_PORT 6060 main( int argc, char **argv ) int sd; /* Socket descriptor */ struct sockaddr_in server; /* Server to connect */ struct hostent *server_host; /* Host info */ char buf[bufsize]; int nbytes; /* Create socket */ if ( (sd=socket(af_inet, SOCK_STREAM, 0)) < 0 ) fprintf(stderr, "%s: cannot create socket: ", argv[0]); /* Get info on host */ if ( (server_host=gethostbyname(argv[1])) == NULL ) fprintf(stderr, "%s: unknown host %s\n", argv[0], argv[1]); /* Set up struct sockaddr_in */ server.sin_family = AF_INET; server.sin_port = SERVER_PORT; bcopy((char*)server_host->h_addr, (char*)&server.sin_addr, server_host->h_length); /* Connect */ if ( connect(sd, &server, sizeof(server)) < 0 ) fprintf(stderr, "%s: cannot connect to server: ", argv[0]); /* Get date */ if ( (nbytes=read(sd, buf, BUFSIZE-1)) <= 0 ) fprintf(stderr, "%s: read failed: ", argv[0]);

buf[nbytes] = 0; printf("date on host %s is: %s\n", argv[1], buf); close(sd); exit(0);

#include <sys/types.h> #include <sys/time.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <string.h> #include <unistd.h> #include <errno.h> #include <signal.h> #include <stdio.h> #include <time.h> #include <stdlib.h> Channel #define BUFSIZE 1024 #define PORT 4610 #define SERVER_PORT 4613 double pl,pd,u; main( int argc, char **argv ) int new_sd,sd2,sd3; srand(time(null)); char Rxbuf_from_Rx[BUFSIZE],Rxbuf_from_Tx[BUFSIZE]; int parent,child,nbytes,n1bytes; int channel_delay=4000000; struct sockaddr_in server; struct hostent *server_host; /* Create socket */ if ( (sd2=socket(af_inet, SOCK_STREAM, 0)) < 0 ) fprintf(stderr, "%s: cannot create socket: ", argv[0]); /* Get info on host */ if ( (server_host=gethostbyname(argv[1])) == NULL ) fprintf(stderr, "%s: unknown host %s\n", argv[0], argv[1]); /* Set up struct sockaddr_in */ server.sin_family = AF_INET; server.sin_port = htons(server_port); bcopy((char*)server_host->h_addr, (char*)&server.sin_addr, server_host->h_length); connect(sd2,(struct sockaddr *)&server, sizeof(server)); struct sockaddr_in in_sa; int in_sa_size = sizeof(in_sa); in_sa.sin_family = AF_INET; in_sa.sin_port = htons(port); in_sa.sin_addr.s_addr = htonl(inaddr_any); if ((new_sd =socket(af_inet,sock_stream,0)) < 0) fprintf(stderr,"%s: cannot create listening socket: ",argv[0]); if(bind (new_sd,(struct sockaddr *)&in_sa,in_sa_size)<0) fprintf(stderr,"%s: cannot bind listening socket: ",argv[0]);

// Initialize a listen queque if(listen(new_sd,5)<0) fprintf(stderr, "%s: cannot listen on socket: ",argv[0]); socklen_t len = sizeof(in_sa); if((sd3=accept(new_sd,(struct sockaddr *)&in_sa,&len))<0) signal(sigpipe,sig_ign); printf("\ngive Channel Probability for Loss:"); scanf("%lf",&pl); printf("\ngive Channel Probability for Delay:"); scanf("%lf",&pd); if(fork()==0 ) child=getpid(); while((n1bytes=read(sd2,rxbuf_from_rx,bufsize))>0) Rxbuf_from_Rx[n1bytes] = '\0'; printf("channel Recieved Ack %c from Receiver \n\n", Rxbuf_from_Rx[0]); write(sd3,rxbuf_from_rx,n1bytes); else parent=getppid(); while((nbytes=read(sd3,rxbuf_from_tx,bufsize))>0) Rxbuf_from_Tx[2] = '\0'; printf("channel Recieved from Transmitter: %s\n", Rxbuf_from_Tx); u =1+rand()%100; u=u/100; if (pl>=u) printf("packet %c lost\n", Rxbuf_from_Tx[1]); else if ((pl<u)&&(pd>=u)) printf("packet %c delay for %d microseconds\n", Rxbuf_from_Tx[1], channel_delay); usleep(channel_delay); write(sd2, Rxbuf_from_Tx,nbytes); else if ((pl<u)&&(pd<u)) printf("packet %c send successfully\n", Rxbuf_from_Tx[1]); write(sd2, Rxbuf_from_Tx,nbytes); else printf("unexpected error\n");