Beruflich Dokumente
Kultur Dokumente
Intro
• File I/O based
– open
– create
– close
– read
– write
– lseek
1
A Socket?
A Socket?
• End-point of communication
– ip @
• struct hostent *gethostbyname(char *hostname)
– port number
• Server: www=80, FTP = 21, Telnet = 23, etc…
• Client: Dynamic (1024-5000)
– protocol
• SOCK_STREAM
• SOCK_DGRAM
• SOCK_RAW
– Additional settings (TTL, TOS,… )
2
Functions: Overview
TCP UDP
Server socket() socket() Create Endpoint
bind() [bind()] Bind Address
listen() specify queue
accept() wait for connection
read() recvfrom() data transfer
write() sendto()
recv()
send()
Client socket() socket() Create Endpoint
[bind()] bind() Bind Address
connect() establish connection
socket()
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
3
bind()
#include <sys/types.h>
#include <sys/socket.h>
connect()
#include <sys/types.h>
#include <sys/socket.h>
Note:
* CO client doesn’t need bind before calling connect!
* CL client can use connect() so you can use send()
rather than sendto()
4
listen()
int listen(int sockfd, int backlog);
accept()
#include <sys/types.h>
#include <sys/socket.h>
Blocking Call!!!
5
send, sendto, recv and recvfrom
#include <sys/types.h>
#include <sys/socket.h>
struct sockaddr
Generic Address structure:
Defined in <sys/socket.h>
struct sockaddr {
sa_family_t sa_family; /* address family */
char sa_data[14]; /* up to 14 bytes of direct address */
};
In TCP/IP we use
struct sockaddr_in {
sa_family_t sin_family;
in_port_t sin_port;
struct in_addr sin_addr;
unsigned char sin_zero[8];
};
6
struct in_addr
struct in_addr {
union {
struct { uchar_t s_b1, s_b2, s_b3, s_b4; } _S_un_b;
struct { ushort_t s_w1, s_w2; } _S_un_w;
uint32_t _S_addr;
} _S_un;
#define s_addr _S_un._S_addr
};
A final note...
Compiling with gcc:
With VC++