#include <stdio.h>
void byteorder()
{
    union
    {
        short value;
        char union_bytes[ sizeof( short ) ];
    } test;
    test.value = 0x0102;
    if (  ( test.union_bytes[ 0 ] == 1 ) && ( test.union_bytes[ 1 ] == 2 ) )
    {
        printf( "big endian\n" );
    }
    else if ( ( test.union_bytes[ 0 ] == 2 ) && ( test.union_bytes[ 1 ] == 1 ) )
    {
        printf( "little endian\n" );
    }
    else
    {
        printf( "unknown...\n" );
    }
}

一些工具代码,不错

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
static bool stop = false;
static void handle_term( int sig )
{
    stop = true;
}
int main( int argc, char* argv[] )
{
    signal( SIGTERM, handle_term );//运行kill命令,会产生term信号
    if( argc <= 3 )
    {
        printf( "usage: %s ip_address port_number backlog\n", basename( argv[0] ) );
        return 1;
    }
    const char* ip = argv[1];
    int port = atoi( argv[2] );
    int backlog = atoi( argv[3] );
    int sock = socket( PF_INET, SOCK_STREAM, 0 );
    assert( sock >= 0 );
    struct sockaddr_in address;
    bzero( &address, sizeof( address ) );
    address.sin_family = AF_INET;
    inet_pton( AF_INET, ip, &address.sin_addr );
    address.sin_port = htons( port );
    int ret = bind( sock, ( struct sockaddr* )&address, sizeof( address ) );
    assert( ret != -1 );
    ret = listen( sock, backlog );
    assert( ret != -1 );
    while ( ! stop )
    {
        sleep( 1 );
    }//运行kill就不循环休眠了,
    close( sock );
    return 0;
}


#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
int main( int argc, char* argv[] )
{
    if( argc <= 2 )
    {
        printf( "usage: %s ip_address port_number\n", basename( argv[0] ) );
        return 1;
    }
    const char* ip = argv[1];
    int port = atoi( argv[2] );
    struct sockaddr_in address;
    bzero( &address, sizeof( address ) );
    address.sin_family = AF_INET;
    inet_pton( AF_INET, ip, &address.sin_addr );
    address.sin_port = htons( port );
    int sock = socket( PF_INET, SOCK_STREAM, 0 );
    assert( sock >= 0 );
    int ret = bind( sock, ( struct sockaddr* )&address, sizeof( address ) );
    assert( ret != -1 );
    ret = listen( sock, 5 );
    assert( ret != -1 );
    struct sockaddr_in client;
    socklen_t client_addrlength = sizeof( client );
    int connfd = accept( sock, ( struct sockaddr* )&client, &client_addrlength );
    if ( connfd < 0 )
    {
        printf( "errno is: %d\n", errno );
    }
    else
    {
        char remote[INET_ADDRSTRLEN ];
        printf( "connected with ip: %s and port: %d\n",
            inet_ntop( AF_INET, &client.sin_addr, remote, INET_ADDRSTRLEN ), ntohs( client.sin_port ) );
            //这个函数这么坑爹。这么长。。还有很多其他函数可以实现
        close( connfd );
    }
    close( sock );
    return 0;
}


#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main( int argc, char* argv[] )
{
    if( argc <= 2 )
    {
        printf( "usage: %s ip_address port_number\n", basename( argv[0] ) );
        return 1;
    }
    const char* ip = argv[1];
    int port = atoi( argv[2] );
    struct sockaddr_in server_address;
    bzero( &server_address, sizeof( server_address ) );
    server_address.sin_family = AF_INET;
    inet_pton( AF_INET, ip, &server_address.sin_addr );
    server_address.sin_port = htons( port );
    int sockfd = socket( PF_INET, SOCK_STREAM, 0 );
    assert( sockfd >= 0 );
    if ( connect( sockfd, ( struct sockaddr* )&server_address, sizeof( server_address ) ) < 0 )
    {
        printf( "connection failed\n" );
    }
    else
    {
        printf( "send oob data out\n" );
        const char* oob_data = "abc";
        const char* normal_data = "123";
        send( sockfd, normal_data, strlen( normal_data ), 0 );
        send( sockfd, oob_data, strlen( oob_data ), MSG_OOB );//带外数据。。没用过。
        send( sockfd, normal_data, strlen( normal_data ), 0 );
    }
    close( sockfd );
    return 0;
}

带外数据标记。

现在进程使用以MSG_OOB 为参数的send()函数写入一个单字节的"带外数据",包
含一个ASCII 字符"a":
send(fd, “a”, 1, MSG_OOB);
TCP 将数据放在下一个可用的发送缓冲区中,并设置这个连接的"紧急指针"(urgent
pointer)指向下一个可用的缓冲区空间.图6-11 表示了我们描述的这个状态,并将带外数
据(Out-Of-Band)表示为"OOB"。


这个是发送和接收数据时的一个选择值,如果用它则表示需要发送和接收带外数据。带外数据(也称为TCP紧急数据),可以在应用程序中用来处理紧急数据。

协议的实现为了提高效率,往往在应用层传来少量的数据时不马上发送,而是等到数据缓冲区里有了一定量的数据时才一起发送,但有些应用本身数据量并不多,而且需要马上发送,这时,就用紧急指针,这样数据就会马上发送,而不需等待有大量的数据。相当优先级别最高,通过带外数据发送。接受时候可以设置标记优先结构带外数据。

通常情况下都是采用heartbeat来持续检测连接的,如果不希望采用这种方式的话,也即在没有任何数据发送和接收的情况下,tcp栈需要大概一个小时的时间才能发现连接已经不存在。 定时发送带外消息


如在某些连接中进行强制中 当你按下CTRL+C时
可能出现的就是这种情况
至于说紧急数据是独立的 可能就不是了
它可能还是和其他数据有关系的
只是出现紧急情况时 把它作为带外数据提前发送而已

带外信令:是指发送数据的通道和发送控制信令的通道不相同,即控制信令的传输不会占用数据通道的带宽(也就是所谓的“带外”),ISDN的B(HDLC或PPP协议)、D(LAPD协议)信道分别传输数据和控制信令。

带内信令:也叫做强取比特信令,是指发送数据的通道和发送控制信令的通道相同。例如,一根线使用64 kbps中的8 kbps信道用于WAN同步和信令。剩下的56 kbps处理用户数据的传输。

阻塞模式与非阻塞模式下write的返回值各代表什么意思? 有没有区别?

阻 塞与非阻塞write返回值没有区分,都是 <0:出错,=0:连接关闭,>0发送数据大小,特别:返回值 <0时并且 (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)的情况下认为连接是正常的, 继续发送。只是阻塞模式下write会阻塞着发送数据,非阻塞模式下如果暂时无法发送数据会返回,不会阻塞着 write,因此需要循环发送


#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#define BUF_SIZE 1024
int main( int argc, char* argv[] )
{
    if( argc <= 2 )
    {
        printf( "usage: %s ip_address port_number\n", basename( argv[0] ) );
        return 1;
    }
    const char* ip = argv[1];
    int port = atoi( argv[2] );
    struct sockaddr_in address;
    bzero( &address, sizeof( address ) );
    address.sin_family = AF_INET;
    inet_pton( AF_INET, ip, &address.sin_addr );
    address.sin_port = htons( port );
    int sock = socket( PF_INET, SOCK_STREAM, 0 );
    assert( sock >= 0 );
    int ret = bind( sock, ( struct sockaddr* )&address, sizeof( address ) );
    assert( ret != -1 );
    ret = listen( sock, 5 );
    assert( ret != -1 );
    struct sockaddr_in client;
    socklen_t client_addrlength = sizeof( client );
    int connfd = accept( sock, ( struct sockaddr* )&client, &client_addrlength );
    if ( connfd < 0 )
    {
        printf( "errno is: %d\n", errno );
    }
    else
    {
        char buffer[ BUF_SIZE ];
        memset( buffer, '\0', BUF_SIZE );
        ret = recv( connfd, buffer, BUF_SIZE-1, 0 );
        printf( "got %d bytes of normal data '%s'\n", ret, buffer );
        memset( buffer, '\0', BUF_SIZE );
        ret = recv( connfd, buffer, BUF_SIZE-1, MSG_OOB );//专门对应上一个例子
        printf( "got %d bytes of oob data '%s'\n", ret, buffer );
        memset( buffer, '\0', BUF_SIZE );
        ret = recv( connfd, buffer, BUF_SIZE-1, 0 );
        printf( "got %d bytes of normal data '%s'\n", ret, buffer );
        close( connfd );
    }
    close( sock );
    return 0;
}
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
int main( int argc, char* argv[] )
{
    if( argc <= 2 )
    {
        printf( "usage: %s ip_address port_number\n", basename( argv[0] ) );
        return 1;
    }
    const char* ip = argv[1];
    int port = atoi( argv[2] );
    int sock = socket( PF_INET, SOCK_STREAM, 0 );
    assert( sock >= 0 );
    int reuse = 1;
    setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof( reuse ) );//地址复用,基本知识
    struct sockaddr_in address;
    bzero( &address, sizeof( address ) );
    address.sin_family = AF_INET;
    inet_pton( AF_INET, ip, &address.sin_addr );
    address.sin_port = htons( port );
    int ret = bind( sock, ( struct sockaddr* )&address, sizeof( address ) );
    assert( ret != -1 );
    ret = listen( sock, 5 );
    assert( ret != -1 );
    struct sockaddr_in client;
    socklen_t client_addrlength = sizeof( client );
    int connfd = accept( sock, ( struct sockaddr* )&client, &client_addrlength );
    if ( connfd < 0 )
    {
        printf( "errno is: %d\n", errno );
    }
    else
    {
        char remote[INET_ADDRSTRLEN ];
        printf( "connected with ip: %s and port: %d\n",
            inet_ntop( AF_INET, &client.sin_addr, remote, INET_ADDRSTRLEN ), ntohs( client.sin_port ) );
        close( connfd );
    }
    close( sock );
    return 0;
}
#include <sys/socket.h>
#include <arpa/inet.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define BUFFER_SIZE 512
int main( int argc, char* argv[] )
{
    if( argc <= 3 )
    {
        printf( "usage: %s ip_address port_number send_bufer_size\n", basename( argv[0] ) );
        return 1;
    }
    const char* ip = argv[1];
    int port = atoi( argv[2] );
    struct sockaddr_in server_address;
    bzero( &server_address, sizeof( server_address ) );
    server_address.sin_family = AF_INET;
    inet_pton( AF_INET, ip, &server_address.sin_addr );
    server_address.sin_port = htons( port );
    int sock = socket( PF_INET, SOCK_STREAM, 0 );
    assert( sock >= 0 );
    int sendbuf = atoi( argv[3] );
    int len = sizeof( sendbuf );
    //设置发送缓冲区
    setsockopt( sock, SOL_SOCKET, SO_SNDBUF, &sendbuf, sizeof( sendbuf ) );
    getsockopt( sock, SOL_SOCKET, SO_SNDBUF, &sendbuf, ( socklen_t* )&len );
    printf( "the tcp send buffer size after setting is %d\n", sendbuf );
    if ( connect( sock, ( struct sockaddr* )&server_address, sizeof( server_address ) ) != -1 )
    {
        char buffer[ BUFFER_SIZE ];
        memset( buffer, 'a', BUFFER_SIZE );
        send( sock, buffer, BUFFER_SIZE, 0 );
    }
    close( sock );
    return 0;
}
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
int main( int argc, char *argv[] )
{
    assert( argc == 2 );
    char *host = argv[1];
    struct hostent* hostinfo = gethostbyname( host );//平时都不用过。。
    assert( hostinfo );
    struct servent* servinfo = getservbyname( "daytime", "tcp" );//这是个什么服务名?》哪里来的?本机系统自带的?
    assert( servinfo );
    printf( "daytime port is %d\n", ntohs( servinfo->s_port ) );
    struct sockaddr_in address;
    address.sin_family = AF_INET;
    address.sin_port = servinfo->s_port;
    address.sin_addr = *( struct in_addr* )*hostinfo->h_addr_list;
    int sockfd = socket( AF_INET, SOCK_STREAM, 0 );
    int result = connect( sockfd, (struct sockaddr* )&address, sizeof( address ) );
    assert( result != -1 );
    char buffer[128];
    result = read( sockfd, buffer, sizeof( buffer ) );
    assert( result > 0 );
    buffer[ result ] = '\0';
    printf( "the day tiem is: %s", buffer );
    close( sockfd );
    return 0;
}