![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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? |
|
||||
|
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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|