Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 05-11-2008
Member
 
Join Date: Oct 2008
Posts: 55
Using sockets correctly in unix

Hi rodney Here,i am new to unix programming and had little doubt about on unix "C" sockets Hopefully this is a good place to ask this question. I was looking for some advice on how best to use unix c sockets. I'm writing a simple http client, so currently I create a socket with the socket() method, and then call connect() to connect to the server (I'm using a TCP stream, rather than UDP). What I want to know is should I create a new socket using socket(), every time I want to connect to a new server, or can I just close the connection, and call connect() to connect to a new server?

For example.Currently I create the socket, and connect to www.google.com. then I make a standard HTTP Get request for index.htm.This returns a 302 (Resource moved to www.google.co.uk. Now to connect to www.google.co.uk, currently I'm closing my socket,then I use socket() to create a new one, and finally call connect() to connectwww.google.co.uk. Is it possible to just close() the socket, and then call connect() passing the credentials for www.google.co.uk ? I tried this, but it seemed to fail to connect.Originally I assumed that I should always pair a connect() with close(), but perhaps I should pair socket() with close() ?
Reply With Quote
  #2  
Old 05-11-2008
Member
 
Join Date: Oct 2008
Posts: 11
Re: Using sockets correctly in unix

No, because 'close' makes the file descriptor invalid since the socket is now gone. There is nothing to 'connect'.When you are finished with a socket and no longer need it, 'close' it. If you fully 'shutdown' the connection, the socket you are left with cannot be used for anything. All you can do is 'close' it.
hope it helps you.
Reply With Quote
  #3  
Old 05-11-2008
Member
 
Join Date: Oct 2008
Posts: 55
Re: Using sockets correctly in unix

Thanks, that the clarification that I was looking for.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags:



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Using sockets correctly in unix"
Thread Thread Starter Forum Replies Last Post
First mainboard created with two sockets Anna_relic Motherboard Processor & RAM 2 04-01-2012 07:41 PM
AM2+ vs AM3 sockets Xymaya Hardware Peripherals 6 21-11-2010 05:39 AM
Problem with Sockets and Protocols Logan 2 Software Development 5 11-02-2010 06:23 AM
Binary sockets in C# CheckMeNot Software Development 3 18-11-2009 11:02 AM
Sockets in python Benito Software Development 2 19-08-2008 01:15 PM


All times are GMT +5.5. The time now is 04:16 AM.