Unix Socket


 
Thread Tools Search this Thread
Top Forums Programming Unix Socket
# 1  
Old 10-18-2011
Unix Socket

Hai every one ,,,
I am runinng one tcp socket in non-blocking mode. In the connect state(operation in progress) if the server is not running ,client socket select will give success.
FD_ISSET(socketfd,&wfd) will give me succes always and am getting wfd value as 16. I tried with blocking mode it is working fine .....
# 2  
Old 10-18-2011
I believe if you put a socket into non-blocking mode before issuing the connect call, then once the connect succeeds or fails it'll be "writable". I believe you have to check for a socket error to see if the connect actually succeeded.

Code:
int socket_errno = 0;
int optlen = sizeof (socket_errno);

getsockopt (sock_fd, SOL_SOCKET, SO_ERROR, &socket_errno, &optlen);

if (socket_errno == 0)
{
  /* connect ok */
}
else
{
  /* did not connect */
  close(sock_fd);
}

That should do you....
# 3  
Old 10-18-2011
Quote:
Originally Posted by andrew.paul
Hai every one ,,,
I am runinng one tcp socket in non-blocking mode. In the connect state(operation in progress) if the server is not running ,client socket select will give success.
FD_ISSET(socketfd,&wfd) will give me succes always and am getting wfd value as 16. I tried with blocking mode it is working fine .....
select() is there to tell you whether things will block or not, whic makes O_NONBLOCK rather redundant. Probably shouldn't use both.
# 4  
Old 10-18-2011
Quote:
Originally Posted by Corona688
select() is there to tell you whether things will block or not, whic makes O_NONBLOCK rather redundant. Probably shouldn't use both.
???

You have to put a socket into non-blocking mode to use select/poll. Well, you don't have to, but there have been bugs reported against some implementations that will cause select/poll to behave unpredictably or incorrectly if a blocking socket is placed into the select/poll list. So, you should use both.

Further, select is advisory only. So, even if select tells you that the socket "won't block" because it is readable/writeable, when you turn around and try to read/write more than is available in the appropriate TCP buffer from/to a blocking socket the call will still block. Select just tells you the socket can be read/written from/to, more accurately (for most implementations) it tells you the watermark set on the socket has been met for the operation. Typically that watermark is set at 1 byte for both read and write. Therefore, once a single byte can be read or written to the socket select/poll says it's readable/writable. So, if you immediately tried to read/write more than a byte, it's certainly possible (again if you didn't use O_NONBLOCK) that the call could block.

That said, in most implementations these watermarks can be changed. Some developers have tried using that to make their applications more efficient. For example, if knew a header (of size sz) needed to be read at the moment, I could set the read low water mark to sz. Select wouldn't say the socket was readable until sz bytes were available. Of course, this assumes there is enough TCP send/receive space to hold that. Unfortunately, I read some report that said their attempts to improve efficiency this way were thwarted by the fact that constantly changing the watermark was not efficient enough to be worth it.

Anyway...that's a little off topic, but the point is, I don't know how you can say you shouldn't use both. You should, and in some cases, have to use both.
These 2 Users Gave Thanks to DreamWarrior For This Post:
# 5  
Old 10-18-2011
I find you are correct. Spurious data for instance can cause a false ready notification. I've used select() more with terminals than I have with sockets, it seems I have more to learn.
# 6  
Old 10-19-2011
Quote:
Originally Posted by Corona688
I find you are correct. Spurious data for instance can cause a false ready notification. I've used select() more with terminals than I have with sockets, it seems I have more to learn.
We always do. Once someone feels they know everything, they know nothing.
# 7  
Old 10-19-2011
Thanks Warrior it is working fine
Login or Register to Ask a Question

Previous Thread | Next Thread

10 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

unix socket programming

Hi all, my name is anjali, working as an intern, doing masters i have an assignment from my school, which i am not able to solve, can someone please help me in finding the solution for this problem thanking you in advance....lol Write a C socket program on UNIX where a TCP client... (1 Reply)
Discussion started by: anjali.symantec
1 Replies

3. UNIX for Advanced & Expert Users

C socket program on UNIX

Write a C socket program on UNIX where a TCP client sends/reads a time in decimal 24 hours format to the server and the server echoes the seconds, minutes, and hours in the time. Example: Client sends 18.78 hours and the server displays 18 hours, 46 minutes and 48 seconds. (1 Reply)
Discussion started by: adi_always4u143
1 Replies

4. IP Networking

Help with Unix socket programing

hi I am strucked in a client server program client need to login to server client logins if only username and password are correct i have written a program username is stored as file and password is smilar to username whic is stored in that file when server asks for username... (2 Replies)
Discussion started by: karthik1238
2 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

Socket Handling Differences Between Linux & Unix?

Sorry if this is a stupid question! I have been developing a Java application that I am deploying on both Unix and Linux servers, which uses lots of socket handling. When the server side connection is dropped by the server un-gracefully I have been seeing close_waits and null connections. ... (0 Replies)
Discussion started by: Vinnie
0 Replies

7. UNIX for Dummies Questions & Answers

Checking who is listening on a Unix Domain Socket

Hi all, I'm writing a kernel module and part of it involves controlling IPCs between processes. My problem is when a process tries to connect to a Unix domain socket, the only identifying information of the socket it supplies (that I can see, anyway) is the special pathname of the socket. From... (0 Replies)
Discussion started by: blank87
0 Replies

8. UNIX for Advanced & Expert Users

Unix: socket & Co

Hello, I need help to replace the ................. of client.c that request the server implemented by server.c ------------------ Listing 1 - server.c /* Inclusion des différentes librairies nécessaires */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> ... (0 Replies)
Discussion started by: bounkolh
0 Replies

9. HP-UX

Problem in HP-Unix while writing into socket

Our system is having a server and multiple clients. We are monitoring the client FDs using select() system call in HP-UX. After establishing connection-using socket with the remote client, before start sending the data we are checking the status of socket using select( ) call. For first 16... (0 Replies)
Discussion started by: AshokG
0 Replies

10. Programming

How can I program socket in unix?

Excuse me . I'm a beginner . In windows , MFC can be used , but how to do in Unix ? And does unix support c++like VC++ ? How can I get developing tools in Unix ? (7 Replies)
Discussion started by: sanjohn
7 Replies
Login or Register to Ask a Question