Sponsored Content
Full Discussion: Socket programming in C
Top Forums Programming Socket programming in C Post 302303263 by Ahmed waheed on Thursday 2nd of April 2009 08:51:56 AM
Old 04-02-2009
Bug

you can use this working code :

Code:
void etherSetup(char *strIP,int nPort)
{
	unsigned long ip;
	
	if((*strIP <= '9') && (*strIP >= '0'))
		{
			if( (int)(ip = inet_addr(strIP)) == -1 )
				{
				printf("\r\nIP-address must be of the form a.b.c.d\n");
              	exit (2);
				}


		}
	else
		{
			server = gethostbyname(strIP);
    		if(!server) 
				{ 
				printf("\nError in host Name.\n");
				exit(2);
				}
   			ip = *(unsigned long*)(server->h_addr);
		}

/////printf("\r\nSERVER IP=%d\r\n ",ip);



	server = gethostbyaddr((char *)&ip,sizeof(ip),AF_INET);
	if (server == NULL)
		{
        	printf("\r\nERROR, no such host .\n");
        	exit(1);
    	}

	bzero((char *) &m_sockaddr_in, sizeof(m_sockaddr_in));

	m_sockaddr_in.sin_family = AF_INET;
	m_sockaddr_in.sin_port = htons(nPort);
	m_sockaddr_in.sin_addr = *(in_addr*)&ip;
	printf("remotehost : %s   , port : %d \n",strIP,nPort);
}

//-----------------------------------------------------------------
int Create()
{
  sockfd = socket(AF_INET, SOCK_STREAM,IPPROTO_TCP);
  if ( sockfd <0 ) return -1;
  return 0;
}
//-----------------------------------------------------------------
int Connect()
{
	int ret;

  	ret = connect(sockfd,(sockaddr*)&m_sockaddr_in,sizeof(m_sockaddr_in));
  	if ( ret <0 ) return -1; //ERROR in connection>
  	return 0;
}

this is for TCP/IP protocol
 

10 More Discussions You Might Find Interesting

1. Programming

Socket Programming

Dear Reader, Is there any way to check up socket status other than 'netstatus ' Thanks in advance, (1 Reply)
Discussion started by: joseph_shibu
1 Replies

2. Programming

Socket Programming socket

Hello, I actually try to make client-server program. I'm using SCO OpenServer Release 5.0.0 and when I try to compile my code (by TELNET) I've got this error : I'm just using this simple code : and I get the same error if I use : If someone can help me, Thanks (2 Replies)
Discussion started by: soshell
2 Replies

3. Programming

Need Help Regarding Socket Programming

Can anyone plz me. I need a sample code for the following description. Its urgent. It is C/Socket program with the following descriptions: NAME coreadServer - Concurrent Readers Server. coreadClient - Concurrent Readers Client. SYNOPSIS coreadServer <OutputFile> coreadClient <n>... (1 Reply)
Discussion started by: priya.vmr
1 Replies

4. IP Networking

socket programming

my system is a stand alone system... i want to try doing socket porgramming..ihave heard that this is usually done during testing... how can i do that....? (6 Replies)
Discussion started by: damn_bkb
6 Replies

5. IP Networking

socket programming

Hello Everyone Iam working on tcp/ip programming.with some time interval server has to send data.client has to close the connection and to open the connection between the time interval.this is the scenario when iam closing the connection in client side the connection terminates.how to... (1 Reply)
Discussion started by: sureshvaikuntam
1 Replies

6. Programming

help regarding socket programming

i m using sockets for setting up a connection between a server and a client. When the clients gets connected to the server, its ip is conveyed to the server through one of the predefined structures in c library... i save this ip address in an array....1st client's ip address goes to the zeroth... (1 Reply)
Discussion started by: abmxla007
1 Replies

7. UNIX for Advanced & Expert Users

socket programming

can we send udp message to a destination ip address .. without having an ip address configured in our machine using recvfrom ? (2 Replies)
Discussion started by: Gopi Krishna P
2 Replies

8. UNIX for Dummies Questions & Answers

hi i need help with socket programming

in socket programming how can i : Create for example 3 blank files, namely: server, client, network •Server: act as servers/provider, will receive all requests from different client •Client: requesters •Network: middle-layer of communication between server & client any tips or... (6 Replies)
Discussion started by: kedah160
6 Replies

9. Programming

Socket programming

Hi everyone, I'm new to this forum. I'm working on new project for last few days and this forum already helped me on couple of occasions. I don't have any prior experience with network programming so I'll appreciate any advise given. I'm trying to do the following: 1. open user... (2 Replies)
Discussion started by: _thomas
2 Replies

10. Programming

socket programming

how to include socket.h in visual studio 2005.. (2 Replies)
Discussion started by: asd123
2 Replies
GETHOSTBYNAME(3)					   BSD Library Functions Manual 					  GETHOSTBYNAME(3)

NAME
gethostbyname, gethostbyname2, gethostbyaddr, gethostent, sethostent, endhostent, herror, hstrerror -- get network host entry LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <netdb.h> int h_errno; struct hostent * gethostbyname(const char *name); struct hostent * gethostbyname2(const char *name, int af); struct hostent * gethostbyaddr(const void *addr, socklen_t len, int type); struct hostent * gethostent(void); void sethostent(int stayopen); void endhostent(void); void herror(const char *string); const char * hstrerror(int err); DESCRIPTION
The getaddrinfo(3) and getnameinfo(3) functions are preferred over the gethostbyname(), gethostbyname2(), and gethostbyaddr() functions. The gethostbyname(), gethostbyname2() and gethostbyaddr() functions each return a pointer to an object with the following structure describ- ing an internet host referenced by name or by address, respectively. The name argument passed to gethostbyname() or gethostbyname2() should point to a NUL-terminated hostname. The addr argument passed to gethostbyaddr() should point to an address which is len bytes long, in binary form (i.e., not an IP address in human readable ASCII form). The type argument specifies the address family (e.g. AF_INET, AF_INET6, etc.) of this address. The structure returned contains information obtained from mDNSResponder(8), including records in /etc/hosts. struct hostent { char *h_name; /* official name of host */ char **h_aliases; /* alias list */ int h_addrtype; /* host address type */ int h_length; /* length of address */ char **h_addr_list; /* list of addresses from name server */ }; #define h_addr h_addr_list[0] /* address, for backward compatibility */ The members of this structure are: h_name Official name of the host. h_aliases A NULL-terminated array of alternate names for the host. h_addrtype The type of address being returned; usually AF_INET. h_length The length, in bytes, of the address. h_addr_list A NULL-terminated array of network addresses for the host. Host addresses are returned in network byte order. h_addr The first address in h_addr_list; this is for backward compatibility. When using the nameserver, gethostbyname() and gethostbyname2() will search for the named host in the current domain and its parents unless the name ends in a dot. The gethostbyname2() function is an evolution of gethostbyname() which is intended to allow lookups in address families other than AF_INET, for example AF_INET6. The herror() function writes a message to the diagnostic output consisting of the string argument string, the constant string ": ", and a message corresponding to the value of h_errno. The hstrerror() function returns a string which is the message text corresponding to the value of the err argument. FILES
/etc/hosts /etc/resolv.conf EXAMPLES
Print out the hostname associated with a specific IP address: const char *ipstr = "127.0.0.1"; struct in_addr ip; struct hostent *hp; if (!inet_aton(ipstr, &ip)) errx(1, "can't parse IP address %s", ipstr); if ((hp = gethostbyaddr((const void *)&ip, sizeof ip, AF_INET)) == NULL) errx(1, "no name associated with %s", ipstr); printf("name associated with %s is %s ", ipstr, hp->h_name); DIAGNOSTICS
Error return status from gethostbyname(), gethostbyname2() and gethostbyaddr() is indicated by return of a NULL pointer. The integer h_errno may then be checked to see whether this is a temporary failure or an invalid or unknown host. The routine herror() can be used to print an error message describing the failure. If its argument string is non-NULL, it is printed, followed by a colon and a space. The error message is printed with a trailing newline. The variable h_errno can have the following values: HOST_NOT_FOUND No such host is known. TRY_AGAIN This is usually a temporary error and means that the local server did not receive a response from an authoritative server. A retry at some later time may succeed. NO_RECOVERY Some unexpected server failure was encountered. This is a non-recoverable error. NO_DATA The requested name is valid but does not have an IP address; this is not a temporary error. This means that the name is known to the name server but there is no address associated with this name. Another type of request to the name server using this domain name will result in an answer; for example, a mail-forwarder may be registered for this domain. SEE ALSO
getaddrinfo(3), getnameinfo(3), inet_aton(3), resolver(3), hosts(5), hostname(7), mDNSResponder(8) CAVEAT
The gethostent() function is defined, and sethostent() and endhostent() are redefined, when Standard C Library (libc, -lc) is built to use only the routines to lookup in /etc/hosts and not the name server. The gethostent() function reads the next line of /etc/hosts, opening the file if necessary. The sethostent() function opens and/or rewinds the file /etc/hosts. If the stayopen argument is non-zero, the file will not be closed after each call to gethostbyname(), gethostbyname2() or gethostbyaddr(). The endhostent() function closes the file. HISTORY
The herror() function appeared in 4.3BSD. The endhostent(), gethostbyaddr(), gethostbyname(), gethostent(), and sethostent() functions appeared in 4.2BSD. The gethostbyname2() function first appeared in BIND version 4.9.4. BUGS
These functions use a thread-specific data storage; if the data is needed for future use, it should be copied before any subsequent calls overwrite it. Though these functions are thread-safe, still it is recommended to use the getaddrinfo(3) family of functions, instead. Only the Internet address format is currently understood. BSD
May 12, 2006 BSD
All times are GMT -4. The time now is 06:08 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy