Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cs288 Intensive Programming in Linux

Similar presentations


Presentation on theme: "Cs288 Intensive Programming in Linux"— Presentation transcript:

1 Cs288 Intensive Programming in Linux
Instructor: C F Yurkoski Section web site: Class 11 6-9-15

2 Today’s xkcd: cfy

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

4 Homework revu cfy

5 cfy

6 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"); cfy

7 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); cfy

8 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) } cfy

9 /. 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); cfy

10 Homework binary trees due next week
You are given: this header file: and this test harness: 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.

11 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 cfy

12 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); } cfy

13 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); } cfy

14 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>: cfy

15 Client /Server on Linux
cfy

16

17 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)

18 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.

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

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

21 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)).

22 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));

23 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

24 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).

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

26 n = read(newsockfd,buffer,255);

27 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);

28 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.

29 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));

30 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

31 In class assignment Write a program called “arabic” which accepts as single command line argument which is cfy

32 homework See moodle cfy


Download ppt "Cs288 Intensive Programming in Linux"

Similar presentations


Ads by Google