#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