A
arnuld
WANTED: To copy some fixed number of characters in one array from another
using strncpy
WHAT I GOT: Its fails to copy
WHAT I WANT TO KNOW: I know where is the problem (SIZE_SERVER_PORT). I
just want to know why changing the size of one array affects the other
array when both arrays are being used independently from each other ?
(Just change SIZE_SERVER_PORT from 5 to 6 and you will see)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
enum {SIZE_SERVER_IP = 16, SIZE_SERVER_PORT = 5};
int socket_connect(const char* ip, const char* port );
int main(void)
{
if( socket_connect("192.168.0.228", "65534") < 0 )
{
printf("IN: %s at %d: Socket Error\n", __FILE__, __LINE__);
return -1;
}
return 0;
}
int socket_connect(const char* ip, const char* port )
{
char servIP[SIZE_SERVER_IP] = {0};
char myport[SIZE_SERVER_PORT] = {0};
int servPort = -1;
int sockfd = -1;
int c ;
struct sockaddr_in server,client;
if( NULL == ip || NULL == port )
{
printf("IN: %s, %s | could not connect to server, either IP or PORT
are NULL or invalid, please check this function first\n", __func__,
__FILE__);
return -1;
}
else
{
printf("Server IP = %s, strlen(IP) = %d\n", ip, strlen(ip));
printf("Server PORT = %s\n", port);
}
strncpy(servIP, ip, SIZE_SERVER_IP);
strcpy(myport, port);
servPort=atoi(myport);
printf("Copied Server IP = %s\n", servIP);
printf("Copied Server PORT = %d\n", servPort);
if(servPort < 0 || NULL == servIP)
{
printf("IN: %s, %s | Invalid PORT/IP\n", __func__, __FILE__);
return -1;
}
memset((char*)&client,0,sizeof(struct sockaddr_in));
memset((char*)&server,0,sizeof(struct sockaddr_in));
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
{
printf("IN: %s, %s | socket call failed:%s\n", __func__, __FILE__,
strerror(errno));
return -1;
}
server.sin_family=AF_INET;
server.sin_port=htons(servPort);
if(inet_pton(AF_INET, servIP, &server.sin_addr) <= 0)
{
printf("IN: %s - %s: INET_PTON ERROR for string: %s\n", __func__,
__FILE__, servIP);
}
c = connect(sockfd,(struct sockaddr *)&server,sizeof(struct
sockaddr_in));
if(c < 0)
{
printf("IN: %s, %s | connect call failed, connect returned = %d\n",
__func__, __FILE__, c);
printf("STRERROR(errono) = %s\n", strerror(errno));
return -1;
}
return sockfd;
}
==================== OUTPUT =============================
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra socket-test.c
[arnuld@dune programs]$ ./a.out
Server IP = 192.168.0.228, strlen(IP) = 13
Server PORT = 65534
Copied Server IP =
Copied Server PORT = 65534
IN: socket_connect - socket-test.c: INET_PTON ERROR for string:
IN: socket_connect, socket-test.c | connect call failed, connect returned
= -1
STRERROR(errono) = Connection refused
IN: socket-test.c at 21: Socket Error
[arnuld@dune programs]$
using strncpy
WHAT I GOT: Its fails to copy
WHAT I WANT TO KNOW: I know where is the problem (SIZE_SERVER_PORT). I
just want to know why changing the size of one array affects the other
array when both arrays are being used independently from each other ?
(Just change SIZE_SERVER_PORT from 5 to 6 and you will see)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
enum {SIZE_SERVER_IP = 16, SIZE_SERVER_PORT = 5};
int socket_connect(const char* ip, const char* port );
int main(void)
{
if( socket_connect("192.168.0.228", "65534") < 0 )
{
printf("IN: %s at %d: Socket Error\n", __FILE__, __LINE__);
return -1;
}
return 0;
}
int socket_connect(const char* ip, const char* port )
{
char servIP[SIZE_SERVER_IP] = {0};
char myport[SIZE_SERVER_PORT] = {0};
int servPort = -1;
int sockfd = -1;
int c ;
struct sockaddr_in server,client;
if( NULL == ip || NULL == port )
{
printf("IN: %s, %s | could not connect to server, either IP or PORT
are NULL or invalid, please check this function first\n", __func__,
__FILE__);
return -1;
}
else
{
printf("Server IP = %s, strlen(IP) = %d\n", ip, strlen(ip));
printf("Server PORT = %s\n", port);
}
strncpy(servIP, ip, SIZE_SERVER_IP);
strcpy(myport, port);
servPort=atoi(myport);
printf("Copied Server IP = %s\n", servIP);
printf("Copied Server PORT = %d\n", servPort);
if(servPort < 0 || NULL == servIP)
{
printf("IN: %s, %s | Invalid PORT/IP\n", __func__, __FILE__);
return -1;
}
memset((char*)&client,0,sizeof(struct sockaddr_in));
memset((char*)&server,0,sizeof(struct sockaddr_in));
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
{
printf("IN: %s, %s | socket call failed:%s\n", __func__, __FILE__,
strerror(errno));
return -1;
}
server.sin_family=AF_INET;
server.sin_port=htons(servPort);
if(inet_pton(AF_INET, servIP, &server.sin_addr) <= 0)
{
printf("IN: %s - %s: INET_PTON ERROR for string: %s\n", __func__,
__FILE__, servIP);
}
c = connect(sockfd,(struct sockaddr *)&server,sizeof(struct
sockaddr_in));
if(c < 0)
{
printf("IN: %s, %s | connect call failed, connect returned = %d\n",
__func__, __FILE__, c);
printf("STRERROR(errono) = %s\n", strerror(errno));
return -1;
}
return sockfd;
}
==================== OUTPUT =============================
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra socket-test.c
[arnuld@dune programs]$ ./a.out
Server IP = 192.168.0.228, strlen(IP) = 13
Server PORT = 65534
Copied Server IP =
Copied Server PORT = 65534
IN: socket_connect - socket-test.c: INET_PTON ERROR for string:
IN: socket_connect, socket-test.c | connect call failed, connect returned
= -1
STRERROR(errono) = Connection refused
IN: socket-test.c at 21: Socket Error
[arnuld@dune programs]$