DOC PREVIEW
Berkeley ELENG 122 - Socket Programming Lec 2

This preview shows page 1-2-3-4-5 out of 14 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Socket Programming Lec 2 Rishi Kant Review of Socket programming Decide which type of socket stream or datagram Based on type create socket using socket function For datagram Define the IP and port you wish to connect from and connect to in 2 separate sockaddr in structures For stream Define the IP and port you wish to listen on server or connect to client in a sockaddr in structure For datagrams and stream servers bind the sockaddr in structure to the socket allocated using the bind function Review of Socket programming For servers use the listen function to signal the OS to monitor incoming connections and use the accept function to wait for a client For clients use the connect function to initiate the 3 way handshake and set up the stream connection to the server For streams use the send and recv functions to transmit and receive bytes For datagram use the sendto and recvfrom functions to transmit and receive packets Example from book pg 33 include stdio h include sys types h include sys socket h include netinet in h include netdb h define PORT 5432 define MAX LINE 256 int main int argc char argv FILE fp struct hostent hp struct sockaddr in sin char host char buf MAX LINE int s int len if argc 2 host argv 1 else fprintf stderr usage simplex talk host n exit 1 hp gethostbyname host if hp fprintf stderr simplex talk unknown host s n host exit 1 Example from book pg 33 bzero char sin sizeof sin sin sin family AF INET bcopy hp h addr char sin sin addr hp h length sin sin port htons PORT if s socket AF INET SOCK STREAM 0 0 perror simple talk socket exit 1 if connect s struct sockaddr sin sizeof sin 0 perror simplex talk connect close s exit 1 while fgets buf sizeof buf stdin buf MAX LINE 1 0 len strlen buf 1 send s buf len 0 Example from book pg 33 include stdio h include sys types h include sys socket h include netinet in h include netdb h define PORT 5432 define MAX PENDING 5 define MAX LINE 256 int main FILE fp struct sockaddr in sin client char buf MAX LINE int s new s int len bzero char sin sizeof sin sin sin family AF INET sin sin addr s addr INADDR ANY sin sin port htons PORT if s socket AF INET SOCK STREAM 0 0 perror simple talk socket exit 1 if bind s struct sockaddr sin sizeof sin 0 Example from book pg 33 perror simplex talk bind close s exit 1 listen s MAX PENDING while 1 if new s accept s struct sockaddr client len 0 perror simplex talk accept exit 1 while len recv new s buf sizeof buf 0 fputs buf stdout close new s How to compile your program gcc o output file list of source files lsocket lnsl e g gcc o project proj c lsocket lnsl Pitfalls Forgetting to convert from host to network byte order and back htons htonl etc Forgetting to check if a function generated an error by checking return value Forgetting to check the number of bytes transmitted received by send recv Forgetting to use the addressof operator Forgetting to include the proper header files Forgetting to flush output streams fflush Forgetting to set the initial value of length before passing it to accept or recvfrom problem I faced but not shown in any of the examples Polling streams Read operations are blocking calls so we need a way to check when a stream is ready to be read from Accomplished by using the select function Very good tutorial http www ecst csuchico edu beej guide net html Polling streams Include files include sys time h include sys types h include unistd h FD ZERO fd set set clears a file descriptor set FD SET int fd fd set set adds fd to the set FD CLR int fd fd set set removes fd from the set FD ISSET int fd fd set set tests to see if fd is in the set int select int numfds fd set readfds fd set writefds fd set exceptfds struct timeval timeout Java Sockets http java sun com docs books tutorial networking sockets index html Socket and ServerSocket classes for TCP DatagramSocket for UDP Clients use the Socket and servers use ServerSocket Simple example Server lines ServerSocket svr new ServerSocket 2000 Socket s Svr accept Client lines Socket s new Socket localhost 2000 Streams s getInputStream s getOutputStream Project Usage Usage There will be exactly 1 program named chitchat that can work in either client or server mode The command line usage will be chitchat port host port If no arguments are specified a usage message should be displayed If 1 argument is specified the program should start up in server mode If 2 arguments are specified the program should start in client mode


View Full Document

Berkeley ELENG 122 - Socket Programming Lec 2

Documents in this Course
Lecture 6

Lecture 6

22 pages

Wireless

Wireless

16 pages

Links

Links

21 pages

Ethernet

Ethernet

10 pages

routing

routing

11 pages

Links

Links

7 pages

Switches

Switches

30 pages

Multicast

Multicast

36 pages

Switches

Switches

18 pages

Security

Security

16 pages

Switches

Switches

18 pages

Lecture 1

Lecture 1

56 pages

OPNET

OPNET

5 pages

Lecture 4

Lecture 4

16 pages

Ethernet

Ethernet

65 pages

Models

Models

30 pages

TCP

TCP

16 pages

Wireless

Wireless

48 pages

Load more
Download Socket Programming Lec 2
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Socket Programming Lec 2 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Socket Programming Lec 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?