sockets in c. 2 references manual pages %torch man –s 3socket … socket, bind, listen, etc....

7
Sockets in C

Upload: virginia-bennett

Post on 05-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Sockets in C. 2 References Manual Pages %torch man –s 3socket … Socket, bind, listen, etc. %torch man –s 3c select Resources section of website

Sockets in C

Page 2: Sockets in C. 2 References Manual Pages %torch man –s 3socket … Socket, bind, listen, etc. %torch man –s 3c select Resources section of website

2

References

Manual Pages %torch man –s 3socket … Socket, bind, listen, etc. %torch man –s 3c select

Resources section of website

Page 3: Sockets in C. 2 References Manual Pages %torch man –s 3socket … Socket, bind, listen, etc. %torch man –s 3c select Resources section of website

3

Java…

Java is a high-level language (HLL) It does a lot of work for the

programmer It hides much of the complexity of n/w

programs This can be very good, but not when

you need to understand what is happening in your programs

Page 4: Sockets in C. 2 References Manual Pages %torch man –s 3socket … Socket, bind, listen, etc. %torch man –s 3c select Resources section of website

4

… and C

C is not exactly a HLLIt has many features that are close to the operating system and processorYet if it is used carefully, with standard libraries, etc., It can be portableIt can also be an unholy mess

Page 5: Sockets in C. 2 References Manual Pages %torch man –s 3socket … Socket, bind, listen, etc. %torch man –s 3c select Resources section of website

5

Steps to Make a Server Socket

1. Make a socketLike opening a file

2. Bind a socketAttach it to a specific port

3. Tell it to accept connections(for stream sockets only)

4. Receive data into a buffer

Page 6: Sockets in C. 2 References Manual Pages %torch man –s 3socket … Socket, bind, listen, etc. %torch man –s 3c select Resources section of website

6

Socket Descriptor Sets

Sockets in C are in groups Called fd_sets (file descriptor sets) The O/S will send a signal when one

of the sockets in your fd_set has data to read

We use these in the accept loop

Page 7: Sockets in C. 2 References Manual Pages %torch man –s 3socket … Socket, bind, listen, etc. %torch man –s 3c select Resources section of website

7

The Accept Loop in C

While (1) { clear all flags set flags to say which sockets to monitor wait for accept() to return read data from socket or sockets

receive send Close

}