Created
June 26, 2017 02:48
-
-
Save caisan/ca7771b02042d0cabc99cf941684c0e1 to your computer and use it in GitHub Desktop.
Simple connect Client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <errno.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <unistd.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
int slow_client( int port ) | |
{ | |
FILE *fp = NULL ; | |
int client_sock ; | |
struct sockaddr_in client_sockaddr ; | |
char buffer[ 30000 + 1 ] ; | |
int file_size ; | |
int total_size ; | |
int read_len ; | |
int write_len ; | |
int nret = 0 ; | |
client_sock = socket( AF_INET , SOCK_STREAM , IPPROTO_TCP ) ; | |
if( client_sock == -1 ) | |
{ | |
printf( "socket failed , errno[%d]\n" , errno ); | |
fclose( fp ); | |
return -1; | |
} | |
memset( & client_sockaddr , 0x00 , sizeof(struct sockaddr_in) ); | |
client_sockaddr.sin_family = AF_INET ; | |
client_sockaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); | |
client_sockaddr.sin_port = htons( (unsigned short)port ); | |
nret = connect( client_sock , (struct sockaddr *) & client_sockaddr , sizeof(struct sockaddr) ) ; | |
if( nret == -1 ) | |
{ | |
printf( "connect failed , errno[%d]\n" , errno ); | |
return -1; | |
} | |
return 0; | |
} | |
int main( int argc , char *argv[] ) | |
{ | |
if( argc != 1 + 1 ) | |
return 7; | |
return -slow_client( atoi(argv[1]) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment