open a socket


 
Thread Tools Search this Thread
Top Forums Programming open a socket
# 1  
Old 03-19-2009
open a socket

hi all,
i meet a problem when opening a socket, is that when the remote host not available or its port hanged ,my program still suspending untill i cancel the operation...
but when the host is ok ,my program work ok.
i handled in my code all these exceptions ,but my problem is when the host is not avaialable i want to exit with that error ???!!!!
here is the part of my code for socket programming :
Code:
void ethernet::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 ethernet::Create()
{
  sockfd = socket(AF_INET, SOCK_STREAM,IPPROTO_TCP);
  if ( sockfd <0 ) return -1;
  return 0;
}
//-----------------------------------------------------------------
int ethernet::Connect()
{
	int ret;

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

N.B:
this code runs under UNIX sun solaris OS
thanks.
# 2  
Old 03-24-2009
By default socket calls are blocking. To make them non blocking, use setsockopt() with a receive timeout. or use ioctl with FIONBIO request.

-Dheeraj
# 3  
Old 03-24-2009
Its also problematic in that sometimes hostname look up does take a few seconds, it has to give it time to look.
# 4  
Old 04-02-2009
how can I use the functions setsockopt() and ioctl() in my code ???!!
# 5  
Old 04-02-2009
take a look at man pages. Also stevens socket programming book is a very good reference.
# 6  
Old 04-02-2009
thank you Gautamdheeraj I appreciate your help .
Is this book available and free on the internet ???
# 7  
Old 04-06-2009
yes, its available over the internet. search ebookee.com for "UNIX Network Programming". You should get some link to download.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. IP Networking

Clarification - Setting socket options at the same time when socket is listening

I need clarification on whether it is okay to set socket options on a listening socket simultaneously when it is being used in an accept() call? Following is the scenario:- -- Task 1 - is executing in a loop - polling a listen socket, lets call it 'fd', (whose file descriptor is global)... (2 Replies)
Discussion started by: jake24
2 Replies

2. Programming

Error with socket operation on non-socket

Dear Experts, i am compiling my code in suse 4.1 which is compiling fine, but at runtime it is showing me for socket programming error no 88 as i searched in errno.h it is telling me socket operation on non socket, what is the meaning of this , how to deal with this error , please... (1 Reply)
Discussion started by: vin_pll
1 Replies

3. Programming

socket function to read a webpage (socket.h)

Why does this socket function only read the first 1440 chars of the stream. Why not the whole stream ? I checked it with gdm and valgrind and everything seems correct... #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> #include... (3 Replies)
Discussion started by: cyler
3 Replies

4. Programming

which socket should socket option on be set

Hi all, On the server side, one socket is used for listening, the others are used for communicating with the client. My question is: if i want to set option for socket, which socket should be set on? If either can be set, what's the different? Again, what's the different if set option... (1 Reply)
Discussion started by: blademan100
1 Replies

5. UNIX for Advanced & Expert Users

connect problem for sctp socket (ipv6 socket) - Runtime fail Invalid Arguments

Hi, I was porting ipv4 application to ipv6; i was done with TCP transports. Now i am facing problem with SCTp transport at runtime. To test SCTP transport I am using following server and client socket programs. Server program runs fine, but client program fails giving Invalid Arguments for... (0 Replies)
Discussion started by: chandrutiptur
0 Replies

6. UNIX for Dummies Questions & Answers

open a socket on a server

Hello, I want to test a firewall rule between 2 servers A & B. I need a command that will open a socket on the server A such as: themagiccommand MyPort And on the client server B, i will run the command telnet IP_serverA Myport Can you help me ? Thank you Gunther (0 Replies)
Discussion started by: gunbol
0 Replies

7. UNIX for Dummies Questions & Answers

how to find the owner PID of open socket on Solaris9?

Hi all, I am trying to connect the open socket and its owner PID on my Solaris9 system. But it seems not very easy. As netstat is not as powerful as it is on Linux platform, without the "-program" option, and "lsof -i <UDP|TCP>@<hostIP>" won't show the one i want although it lists some... (1 Reply)
Discussion started by: sleepy_11
1 Replies

8. UNIX for Dummies Questions & Answers

Which application has a TCP socket open

If I do a netstat -a I can see all the sockets currently open, is there a way that I can tell which application is holding open these sockets ? (3 Replies)
Discussion started by: murphyboy
3 Replies

9. 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
Login or Register to Ask a Question