Create UPD socket (dynamically allocated port number)
来源:互联网 发布:生成二维码的软件 编辑:程序博客网 时间:2024/06/08 12:46
http://www.cs.rutgers.edu/~pxk/417/notes/sockets/udp.html
Step 1. Create a socket
A socket, s
, is created with the socket system call:
All the parameters as well as the return value are integers:
- domain, or address family —
- communication domain in which the socket should be created. Some of address families are AF_INET (IP), AF_INET6 (IPv6), AF_UNIX (local channel, similar to pipes), AF_ISO (ISO protocols), and AF_NS (Xerox Network Systems protocols).
- type —
- type of service. This is selected according to the properties required by the application: SOCK_STREAM (virtual circuit service), SOCK_DGRAM (datagram service), SOCK_RAW (direct IP service). Check with your address family to see whether a particular service is available.
- protocol —
- indicate a specific protocol to use in supporting thesockets operation. This is useful in cases where some families may have more than one protocol to support a given type of service. The return value is a file descriptor (a small integer). The analogy of creating a socket is that of requesting a telephone line from the phone company.
For UDP/IP sockets, we want to specify the IP address family (AF_INET) and datagram service (SOCK_DGRAM). Since there's only one form of datagram service, there are no variations of the protocol, so the last argument, protocol, is zero.
Step 2. Indentify (name) a socket
The system call for bind is:
The first parameter, socket
, is the socket that was created with the socket system call.
For the second parameter, the structure sockaddr
is a generic container that just allows the OS to be able to read the first couple of bytes that identify the address family. The address family determines what variant of the sockaddr
struct to use that contains elements that make sense for that specific communication type. For IP networking, we use struct sockaddr_in
, which is defined in the header netinet/in.h
. This structure defines:
Before calling bind, we need to fill out this structure. The three key parts we need to set are:
- sin_family
- The address family we used when we set up the socket. In our case, it's AF_INET.
- sin_port
- The port number (the transport address). You can explicitly assign a transport address (port) or allow the operating system to assign one. If you're a client and don't need a well-known port that others can use to locate you (since they will only respond to your messages), you can just let the operating system pick any available port number by specifying port 0. If you're a server, you'll generally pick a specific number since clients will need to know a port number to which to address messages.
- sin_addr
- The address for this socket. This is just your machine's IP address. With IP, your machine will have one IP address for each network interface. For example, if your machine has bothWi-Fi and ethernet connections, that machine will have two addresses, one for each interface. Most of the time, we don't care to specify a specific interface and can let the operating system use whatever it wants. The special address for this is 0.0.0.0, defined by the symbolic constant
INADDR_ANY
.
Since the address structure may differ based on the type of transport used, the third parameter specifies the length of that structure. This is simply the size of the internet address structure, sizeof(struct sockaddr_in)
.
- Create UPD socket (dynamically allocated port number)
- dynamically allocated 2d & 3d array
- 动态分配的对象(dynamically allocated object)
- Create class type dynamically
- Dynamically Create Chart Sample
- Create C++ Object Dynamically
- Create menu by javascript dynamically
- Use javascript create checkbox dynamically
- create enum type dynamically(in runtime)
- How to create short-cut dynamically
- Create Proxies Dynamically Using CGLIB Library
- upd
- upd
- What happened if i delete a pointer which was not allocated dynamically?
- filezilla Failed to create listen socket on port 21 for IPv4 解决办法
- common port number
- error:invalid port number
- How to get socket port number if it's system assigned
- ZOJ 1240: IBM Minus One
- ZOJ 1295: Reverse Text
- LeetCode题解:Subsets I and II
- poj 3026 Borg Maze (bfs+prim)
- Textarea自动缩进
- Create UPD socket (dynamically allocated port number)
- 函数传参:引用类型 vs 指针类型
- (学习笔记)安卓的存储方式 之 Sharepreferences
- linux cat /etc/passwd 说明
- GPFS 之 Quorum 深入解析
- Oracle常用函数记录
- linux系统用户以及用户组管理
- 详细解析Linux /etc/passwd文件
- Linux