The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
socket programming pvamsikr UNIX for Advanced & Expert Users 3 11-17-2008 03:39 PM
Socket Programming arunviswanath High Level Programming 7 01-30-2007 03:18 PM
Socket Programming socket soshell High Level Programming 2 06-29-2004 08:49 AM
Socket Programming joseph_shibu High Level Programming 1 11-05-2001 10:37 AM
Socket programming Nadeem Mistry High Level Programming 1 07-23-2001 05:33 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-30-2009
sudharma sudharma is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 18
Socket programming in C

Hi,

I wanted to write a socket program in C, where I always want my socket connected to the server. i.e i shouldn't get disconnected once i get the response back from the server. I want to work in the connected mode, instead of disconnect and connect.

Can somebody please kindly provide me a sample code how i will always be in connected mode to the server. ( Unix enviornment)

Thank you.
Best Regards
Sudharma
  #2 (permalink)  
Old 03-31-2009
pludi's Avatar
pludi pludi is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,944
Program your client so that it doesn't disconnect itself, and tell the server not to disconnect your client when it's done. If that's possible depends on the protocol (of which you haven't told us anything), but HTTP and SMTP, for example, can work like this (although it's not exactly nice to hog up free sockets).
  #3 (permalink)  
Old 03-31-2009
pshaikh pshaikh is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 14
HTTP doesn't keep connection all the time. Isn't it?

@Topic
You can implement a socket program the way you like. Why should anyone want to have connection all the time, even when client and server have no communication going on?
  #4 (permalink)  
Old 04-02-2009
Ahmed waheed Ahmed waheed is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 13
Smile

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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:45 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0