TCP out-of-band data read and write

Source: Internet
Author: User
Tags htons

#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 ("Co    Nnection 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;}

 

#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 1024int 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, ' n ', buf_size);        ret = recv (connfd, buffer, buf_size-1, 0);        printf ("Got%d bytes of normal data '%s ' \ n", ret, buffer);        memset (buffer, ' n ', buf_size);        ret = recv (connfd, buffer, buf_size-1, Msg_oob);        printf ("Got%d bytes of OOB data '%s ' \ n", ret, buffer);        memset (buffer, ' n ', 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;}

  

TCP out-of-band data read and write

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.