1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
#include <sys/socket.h> //for socket(), connect(), sendto() and recvform()
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h> // for printf() and fprintf()
#include <string.h> //for memset()
#include <stdlib.h> //for atoi() and exit()
#include <unistd.h> //for close()
#include <errno.h>
#include <arpa/inet.h> //for sockaddr_in and inet_addr()
#define SERVERPORT 11000
int main(void)
{
int sockfd = 0, n =0;
struct sockaddr_in serv_addr;
//char recvBuff[1024];
if((sockfd=socket(PF_INET, SOCK_STREAM,IPPROTO_TCP))< 0)
{
printf("Cannot create socket\n");
return 1;
}
printf("Already create socket!!!\n");
memset(&serv_addr, '0', sizeof(serv_addr));
// memset(recvBuff, '0', sizeof(recvBuff));
serv_addr.sin_family = AF_INET; //AF_INET is IP address family, Internet = IP addr
serv_addr.sin_addr.s_addr = inet_addr("192.168.1.1"); //sin_addr = ip addr
serv_addr.sin_port = htons(49252); //sin_port = tcp/ip port no
//serv_addr.sin_port =ntohs(49252);
//serv_addr.sin_port = 0;
//memset(&serv_addr, '0', sizeof(serv_addr));
if(connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) < 0)
{
perror("Cannot connect\n");
close(sockfd);
exit (1);
}
else
{
printf("Connected\n");
return 0;
}
return 0;
}
|