TCP/IP 栈
IP数据报
UDP
TCP
互联网地址
网络地址
内网地址
主机地址
整个IP地址
tcpmux |
1 |
TCP |
echo |
7 |
UDP |
echo |
7 |
TCP |
systat |
11 |
TCP |
netstat |
15 |
TCP |
ftp-data |
20 |
TCP File Transfer Protocol (data) |
ftp |
21 |
TCP File Transfer Protocol |
smtp |
25 |
TCP Simple Mail Transfer Protocol |
time |
37 |
TCP Time Server |
time |
37 |
UDP Time Server |
name |
42 |
UDP Name Server |
whois |
43 |
TCP nicname |
domain |
53 |
UDP |
domain |
53 |
TCP |
tftp |
69 |
UDP |
rje |
77 |
TCP |
finger |
79 |
TCP |
link |
87 |
TCP ttylink |
supdup |
95 |
TCP |
hostname |
101 |
TCP hostname |
pop-2 |
109 |
TCP Post Office Protocol |
uucp-path |
117 |
TCP |
nntp |
119 |
TCP Network News Transfer Protocol |
ntp |
123 |
TCP Network Time Protocol |
套接字:
#include <sys/types.h>
#include <sys/socket.h>
int socket(int family, int type, int protocol);
基于连接(TCP)
- Create endpoint (
socket()
) - Bind address (
bind()
) - Specify queue (
listen()
) - Wait for connection (
accept()
) - Transfer data (
read()
/write()
)
- Create endpoint (
socket()
) - Connect to server (
connect()
) - Transfer data (
read()
/write()
)
无连接(UDP)
- Create endpoint (
socket()
) - Bind address (
bind(
)) - Transfer data (
sendto()
/recvfrom()
)
- Create endpoint (
socket()
) - Bind address (
bind()
) (optional ifconnect
is called) - Connect to server (
connect()
) - Transfer data (
sendto()
/recvfrom()
)
使用方法:
OnDataReceived
和OnEvent
这两个函数。默认函数什么都不做。我们现在就创建一个服务器端套接字,并启动它,方法如下:// To use TCP socket
// no smart addressing - we use connection oriented
m_SocketObject.SetSmartAddressing( false );
m_SocketObject.CreateSocket( m_strPort, AF_INET, SOCK_STREAM,0); // TCP
// To use UDP socket
m_SocketObject.SetSmartAddressing( true );
m_SocketObject.CreateSocket( m_strPort,
AF_INET, SOCK_DGRAM, SO_BROADCAST); // UDP
// Now you may start the server/client thread to do the work for you...
m_SocketObject.WatchComm();
// To use TCP socket
m_SocketObject.ConnectTo( strServer, m_strPort, AF_INET, SOCK_STREAM); // TCP
// To use UDP socket
m_SocketObject.ConnectTo( strServer, m_strPort, AF_INET, SOCK_DGRAM); // UDP
// Now you may start the server/client thread to do the work for you...
m_SocketObject.WatchComm();