"Multi-Machine test"
If you don't have more than one computer, you can use the virtual machine
More than one computer, connect the switch
Observing IP
Ipconfig (Linux is ifconfig)
Detection of connectivity
Ping
"Send Receive Buffer"
SendTo ()
A) time lag between depositing data and sending data
b) If the data is stored too fast and too much, the send buffer will be full
Recvfrom ()
A) The OS always keeps the data stored, whether or not it is taken.
b) Recvfrom is to take out the ready-made data from the RECVBUF, if not in time, the buffer is full.
Processing of buffer fills
Send data (two choices)
(1) until the buffer has space left-blocking
(2) The newly sent data is not stored in the buffer
Accept data (two choices)
(1) New data is not accepted
(2) Delete the buffer already has the data, holds the new data
Packet loss phenomenon
Set the size of the Send/Receive buffer
if (1) {int Bufsize=128*1024;int ret=setsockopt (sock.hsock,sol_socket,so_sndbuf,//so_rcvbuf (const char *) & bufsize,sizeof (int));}
"Lost Package"
In general, setting the buffer size does not resolve the packet loss problem
(1) Whether the transmission is uniform
(2) Whether the acceptance is timely
(3) Whether the data bandwidth has exceeded the actual transmission width
In addition, even if the hosta and HOSTB processing is not a problem, any intermediate node device on the network will be able to drop packets when forwarding the packet.
Conclusion: For UDP, the packet loss "routine", the design of the consideration of this point
"Network byte order"
The data on the network is a byte-by-byte serial transmission
"Hello"
h->68
E->65
l->6c
l->6c
o->6f
unsigned int a=0x12345678
It's in the memory arrangement
78 56 34 12
Provisions
Specifies that the low byte is in front, which is called the small end
Specify high-byte in front, then called big-endian
Big and small End is a set (hardware and software), we see the mainstream is small end
such as INTEL/AMD, there are few big-endian systems
"When network is transmitted"
unsigned int a=0x12345678
E.g. 12 34 56 78 (big-endian)
e.g. 78 56 34 12 (small end)
Conventions are transmitted by the big-endian.
Big-endian
An int data is 4 bytes, and the type int data is transmitted by big-endian
Observation
hosta#include<iostream>using namespace std; #include "afos/afsocket.h" void Endian_inttochar (int temp,char Bytes[]) {bytes[0]= (char) (temp >>), bytes[1]= (char) (temp >>), bytes[2]= (char) (temp >> 8); bytes[ 3]= (char) (temp);} int main () {///sender Afsockaddr local ("192.168.1.105", 9000);//Sender Address Afudpsocket Soc;soc. Open (local,true);cout<< "sender-Port 9000," <<endl;while (1) {cout<< "Please push enter!" <<endl;getchar ();//press Enter every time ... AFSOCKADDR Peer ("192.168.1.105", 9001); Char bytes[4];int temp=0x34560012;//int data transfer no longer delve into Endian_inttochar (temp, bytes); Soc. SendTo (Bytes,4,peer);} Soc. Close (); return 0;}
hostb#include<iostream>using namespace std; #include "afos/afsocket.h" int endian_chartoint (char bytes[],int n )//Four bytes char to an int type data {int temp=0;temp+= (bytes[0]<<24); temp+= (bytes[1]<<16); temp+= (bytes[2]<<8 ); temp+= (bytes[3]); return temp;} int main () {///receiver Afsockaddr Local ("192.168.1.105", 9001),//afsockaddr local ("192.168.1.104", 9001);// Receiver Address Afudpsocket Soc;soc. Open (Local,true);cout<< "received-port 9001->" <<endl;char buffer[128];while (1) {afsockaddr peer;int Len=soc. Recvfrom (buffer,128,peer); buffer[len]= ' + '; if (len>0) {int temp;temp=endian_chartoint (Buffer,len);p rintf ("%08x \ n ", temp);} if (strcmp ("Quit", buffer) ==0) {break;}} Soc. Close (); return 0;}
"Default assigned port number"
"Occupancy of the port"
"Multiple senders, one receiver"
"Blocking and timeouts"
"1" The port number assigned by default
The sender can not display the specified port number
Afudpsocket Soc;
Soc. Open ();
You can view the automatically assigned port number
Afsockaddr Local;
Sock. GETLOCALADDR (local);
unsigned int port=local. Getport ();
printf ("Local port:%d\n", port);
Note: The port is automatically assigned when the data is sent.
"2" Port occupancy
When the port is occupied, another socket is created, and the same port is used, the creation fails, sock. Open () return-1
Generally set to reusable
Sock. Open (local,true);//true allows reuse, false does not allow reuse
"3" multiple senders, one receiver
Who sent it?
String Peer_ip=peer. Getip_str ();
int Peer_port=peer. Getport ();
printf ("from:ip->%s port->%d", Peer_ip.c_str (), peer_port);
"4" blocking and timeouts
Sock. Recvfrom () buffer unlimited data waiting, that is, blocking, can set the maximum time
Set the Receive timeout
Sock. Setopt_recvtimeout (3000);//milliseconds set receive timeout
Afudpsocket Soc;
Soc. Setopt_recvtimeout (3000);//milliseconds set receive timeout
int LEN=SOC. Recvfrom (Buffer,128,peer);
if (len<0)
{
printf ("Got%d\n", Len);
}
Non-blocking, timed view (rotation), with data returned 0, no data returned-1
C + + network socket