Cs288 Intensive Programming in Linux

Slides:



Advertisements
Similar presentations
Sockets: Network IPC Internet Socket UNIX Domain Socket.
Advertisements

Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
Today’s topic: Basic TCP API –Socket –Bind –Listen –Connect –Accept –Read –Write –Close.
Lecture 6 TCP Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Tutorial 8 Socket Programming
TDC561 Network Programming Camelia Zlatea, PhD Week 2 – part II: Socket Application Programming Interface.
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
CSE/EE 461 Getting Started with Networking. Basic Concepts  A PROCESS is an executing program somewhere.  Eg, “./a.out”  A MESSAGE contains information.
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)
CS 360 – Spring 2007 Pacific University TCP section 6.5 (Read this section!) 27 Feb 2007.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
ECE453 – Introduction to Computer Networks Lecture 15 – Transport Layer (II)
ECE 4110 – Internetwork Programming Client-Server Model.
Sockets and intro to IO multiplexing. Goals We are going to study sockets programming as means to introduce IO multiplexing problem. We will revisit socket.
Assignment 3 A Client/Server Application: Chatroom.
Elementary TCP Sockets
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
Sirak Kaewjamnong Computer Network Systems
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
---- IT Acumens. COM IT Acumens. COMIT Acumens. COM.
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)
Remote Shell CS230 Project #4 Assigned : Due date :
CSE/EE 461 Getting Started with Networking. 2 Basic Concepts A PROCESS is an executing program somewhere. –Eg, “./a.out” A MESSAGE contains information.
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
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.
Introduction to Sockets
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()
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
UNIX Sockets Outline UNIX sockets CS 640.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
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 Jignesh Patel Palanivel Rathinam connecting processes.
Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
Sockets API Developing Applications using the Sockets API.
Netprog: TCP Sockets1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level.
Socket Programming Client/Server.
Sockets and Beginning Network Programming
Assignment 3 A Client/Server Application: Chatroom
Cs288 Intensive Programming in Linux
Cs288 Intensive Programming in Linux
Network and Sockets Today: Get started doing network programming
Socket Programming in C
Tutorial on Socket Programming
Transport layer API: Socket Programming
UNIX Domain sockets The Linux Programming Interface (ch 57)
Client-side Networking CSE 333 Spring 2018
Network Programming CSC- 341
UNIX Sockets Outline Homework #1 posted by end of day
Network Programming CSC- 341
Socket Programming in C
TCP Sockets Programming
Advanced Network Programming spring 2007
TCP/IP Socket Programming in C
Server-side Programming CSE 333 Autumn 2018
Server-side Programming CSE 333 Summer 2018
Client-side Networking CSE 333 Summer 2018
Socket Programming(1/2)
Internet Networking recitation #8
Outline Communications in Distributed Systems Socket Programming
Sockets.
Today’s topic: Basic TCP API
Presentation transcript:

Cs288 Intensive Programming in Linux Instructor: C F Yurkoski christopher.f.yurkowski@njit.edu Section web site: https://web.njit.edu/~yurkowsk/cs288.html Class 11 6-9-15

Today’s xkcd: 6-9-15 cfy

Today’s agenda Homework revu Questions about upcoming homework Client / Server on Linux In class assignment. 6-9-15 cfy

Homework revu 6-9-15 cfy

6-9-15 cfy

https://web.njit.edu/~yurkowsk/solvesudoku.c readarray(puzzle,array); printf("problem:\n"); printarray(array); if(try(array,0,0)){ printf("solution:\n"); } else printf("could not solve\n"); 6-9-15 cfy

for(z=1;z<10;z++){ if(isvalid(z, array, row, col)){ array[row][col]=z; if(col==8){ if(try(array,row+1,0)) return(1); } else{ if(try(array,row,col+1)) array[row][col]=' '; return(0); 6-9-15 cfy

int isvalid(int num, char a[][9], int row, int col) { int i; / int isvalid(int num, char a[][9], int row, int col) { int i; /* round down to first row and column in block */ int blockrow=3*(row/3); int blockcol=3*(col/3); /* other spaces in block */ int row1=(row+2)%3; int row2=(row+1)%3; int col1=(col+2)%3; int col2=(col+1)%3; #ifdef DEBUG printf("test valid %x\n",num); printarray(a); #endif for(i=0;i<9;i++){ if(a[i][col]==num) return(0); if(a[row][i]==num) } 6-9-15 cfy

/. check the other 4 spaces in this block of 9 /* check the other 4 spaces in this block of 9 */ if(a[row1+blockrow][col1+blockcol]==num) return(0); if(a[row2+blockrow][col1+blockcol]==num) if(a[row1+blockrow][col2+blockcol]==num) if(a[row2+blockrow][col2+blockcol]==num) return(1); 6-9-15 cfy

Homework binary trees due next week You are given: this header file: https://web.njit.edu/~yurkowsk/project2/tree.h and this test harness: https://web.njit.edu/~yurkowsk/project2/main.c Your goal is to implement a pair of binary tree Insert() and Delete() functions which use the function signatures for them in that header file and which successfully links with that test harness. Follow these guidelines: Do not insert duplicates into tree; however, duplicates should not be considered an error, they should be ignored. Do not assume the item you are being asked to delete is actually in the tree. Do not assume that all the deletes will all occur after all the inserts. Submit just the file containing your new Insert() and Delete() routines, not submit the test harness or header file. Assume that the root of the tree on which your functions operate is a global variable named root and that it is initialized to NULL by the test harness prior to the first call to your functions. You must of course also include in your file any additional utility functions you write which your functions use.

content of tree.h typedef struct tree tree; #define MAXWORD 26 struct tree{ struct tree *b4; struct tree *after; char word[MAXWORD]; }; void Insert(char *); void Delete(char *); #ifndef MAIN extern tree *root; #endif 6-9-15 cfy

content of test harness #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAIN 1 #include "tree.h" void printtree(); tree *root=NULL; main(int argc, char **argv) { tree *node; char buf[MAXWORD]; extern tree *root; tree *p; while((scanf("%s",buf))>0) Insert(buf); while(argc-->1) Delete(argv[argc]); printf("Print binary tree in order\n"); printtree(root); } 6-9-15 cfy

test harness (cont.) void printtree(tree *root){ if(root->b4!=NULL) printtree(root->b4); printf("Node is %s \n", root->word); if (root->after!=NULL) printtree( root->after); } 6-9-15 cfy

sample execution of test harness project>: cat - | ./bintree abc xyz 2>/dev/null abc qwe asd zxc Print binary tree in order Node is asd Node is qwe Node is zxc project>: 6-9-15 cfy

Client /Server on Linux 6-9-15 cfy

Client / Server communitation SOCKET(2) Linux Programmer’s Manual SOCKET(2) NAME socket - create an endpoint for communication SYNOPSIS #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> int socket(int domain, int type, int protocol); DESCRIPTION socket() creates an endpoint for communication and returns a descrip- tor. The domain argument specifies a communication domain; this selects the protocol family which will be used for communication. These families are defined in <sys/socket.h>. The currently understood formats include: Name Purpose Man page AF_UNIX, AF_LOCAL Local communication unix(7) AF_INET IPv4 Internet protocols ip(7)

The socket has the indicated type, which specifies the communication semantics. Currently defined types are: SOCK_STREAM Provides sequenced, reliable, two-way, connection-based byte streams. An out-of-band data transmission mecha- nism may be supported. SOCK_DGRAM Supports datagrams (connectionless, unreliable messages of a fixed maximum length). SOCK_SEQPACKET Provides a sequenced, reliable, two-way connection- based data transmission path for datagrams of fixed maximum length; a consumer is required to read an entire packet with each input system call.

Creating a server int sockfd; sockfd = socket(AF_INET, SOCK_STREAM, 0);

Protocol of 0 selects the default, see /usr/lib/x86_64-redhat-linux5E/include/sys/socket.h 6-9-15 cfy

BIND(2) Linux Programmer’s Manual BIND(2) NAME bind - bind a name to a socket SYNOPSIS #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen); DESCRIPTION When a socket is created with socket(2), it exists in a name space (address family) but has no address assigned to it. bind() assigns the address specified to by addr to the socket referred to by the file descriptor sockfd. addrlen specifies the size, in bytes, of the address structure pointed to by addr. Traditionally, this operation is called “assigning a name to a socket”. It is normally necessary to assign a local address using bind() before a SOCK_STREAM socket may receive connections (see accept(2)).

struct sockaddr_in serv_addr; serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = portno; bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));

LISTEN(2) Linux Programmer’s Manual LISTEN(2) NAME listen - listen for connections on a socket SYNOPSIS #include <sys/types.h> #include <sys/socket.h> int listen(int sockfd, int backlog); DESCRIPTION listen() marks the socket referred to by sockfd as a passive socket, that is, as a socket that will be used to accept incoming connection requests using accept(2). The sockfd argument is a file descriptor that refers to a socket of type SOCK_STREAM or SOCK_SEQPACKET. The backlog argument defines the maximum length to which the queue of pending connections for sockfd may grow. If a connection request arrives when the queue is full, the client may receive an error with an indication of ECONNREFUSED or, if the underlying protocol supports

ACCEPT(2) Linux Programmer’s Manual ACCEPT(2) NAME accept - accept a connection on a socket SYNOPSIS #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); #define _GNU_SOURCE int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags); DESCRIPTION The accept() system call is used with connection-based socket types (SOCK_STREAM, SOCK_SEQPACKET).

listen(sockfd,5); newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);

n = read(newsockfd,buffer,255);

sockfd = socket(AF_INET, SOCK_STREAM, 0); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = portno; bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr; listen(sockfd,5); newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); n = read(newsockfd,buffer,255);

Client side CONNECT(2) Linux Programmer’s Manual CONNECT(2) NAME connect - initiate a connection on a socket SYNOPSIS #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); DESCRIPTION The connect() system call connects the socket referred to by the file descriptor sockfd to the address specified by addr. The addrlen argu- ment specifies the size of addr. The format of the address in addr is determined by the address space of the socket sockfd; see socket(2) for further details.

sockfd = socket(AF_INET, SOCK_STREAM, 0); bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; bcopy(server,(char *)&serv_addr.sin_addr.s_addr,length); serv_addr.sin_port = portno; connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)); n = write(sockfd,buffer,strlen(buffer));

afsconnect1-68 ~ >: ./server 6000 & [1] 25153 afsconnect1-69 ~ >: ./client `hostname` 6000 Please enter the message: test 123 Here is the message: test 123 I got your message [1]+ Done ./server 6000

In class assignment Write a program called “arabic” which accepts as single command line argument which is 6-9-15 cfy

homework See moodle 6-9-15 cfy