c_cpp tcp client.c

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp tcp client.c相关的知识,希望对你有一定的参考价值。

// Tell the system what kinds of address we want
struct addrinfo addrCriteria;
memset(&addrCriteria, 0, sizeof(addrCriteria));
addrCriteria.ai_family = AF_INET;
addrCriteria.ai_socktype = SOCK_STREAM;
addrCriteria.ai_protocol = IPPROTO_TCP;

// Lookup a list of matching addresses
struct addrinfo *servAddr;
if ( getaddrinfo( argv[ 1 ], PORT_NUMBER, &addrCriteria, &servAddr) != 0 )
  printf( "Can't get address info\n" );

// Try to just use the first one.
if ( servAddr == NULL )
  printf( "Can't get address\n" );

// Make a socket, and connect it to the address.
int sock = socket( servAddr->ai_family, servAddr->ai_socktype, servAddr->ai_protocol);
if ( sock < 0 )
  printf( "Can't create socket\n" );

// Establish the connection to the echo server
if ( connect( sock, servAddr->ai_addr, servAddr->ai_addrlen ) != 0 )
  printf( "Can't connect to server\n" );

// We're done with the address info now.
freeaddrinfo(servAddr);

// Buffer to send from, don't really care what's in it.
char buffer[ BLOCK_SIZE ];

bytesToSend = 10;
// Try to write to the socket.
int count = write( sock, buffer, bytesToSend );

// Close the socket connection
close( sock );

以上是关于c_cpp tcp client.c的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp client.c

c_cpp TCP链接.C

c_cpp TCP链接.C

c_cpp tcp_server

c_cpp tcp_client

c_cpp TCP_Client