Sponsored Content
Top Forums Programming Using socket to test a TCP port Post 302516979 by Loic Domaigne on Monday 25th of April 2011 04:25:37 PM
Old 04-25-2011
Assuming that you're implementing a TCP client, you have the following possibilities:

1) The connect() is carried out in a separate thread; another thread implements the timer. Upon timeout, the connect thread can be cancelled since connect() is a cancellation point.

2) You raise a signal when a timer expired to the thread calling connect(). This will cause connect() to be interrupted. Only useful for single threaded program; for multi-threaded program use 1) or 3).

3) You use non blocking socket, see a the snippet below
Code:
flags=fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
connect(fd,&addr,len);
...

Then you need to poll() for both read/write events:
Code:
struct pollfd fds;
fds.fd = fd;
fds.events = POLLIN | POLLOUT;
poll(&fds,1,timeout)
...

Error checking is omitted for your convenient (DISCLAIMER: DON'T do this for production code)

HTH, Loïc
 

10 More Discussions You Might Find Interesting

1. Programming

Confusion about TCP/IP socket programming

Hello there chaps. First of all, i'm no TCP/IP-wiz, so forgive me if this is a stupid question. I've been messing around with filetransfer using sockets, and there is one thing that confuses me. This is how it's set up: A server app listens on a port for a client connection. When it... (3 Replies)
Discussion started by: crippe
3 Replies

2. 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

3. AIX

TCP/IP socket binding problem

I have what appears to be a unique socket problem, although admittedly my tcp/ip programming experience is relatively limited. I have a AIX server process using TCP/IP berkely sockets, and a Windows (C#) process. The windows process takes input from a user and sends a message to the Unix... (1 Reply)
Discussion started by: adiaconou
1 Replies

4. Programming

problem receiving data from TCP socket

Hi all, I'm writing a socket program which sends a structure from one machine to another. When I run my client first time it runs well, however after the first time I couldn't receive all the data inside the structure (it is like, half of the array is received and the other half is not set). I... (1 Reply)
Discussion started by: SaTYR
1 Replies

5. IP Networking

Packets sent from Linux TCP socket

Hello, Our software is using a TCP socket (AF_INET, SOCK_STREAM) to communicate with an Ethernet device. When we send a message, the message object writes itself in full onto the socket's stream buffer before the software invokes send() from socket.h. I'm still researching, but have 2... (1 Reply)
Discussion started by: bix_20002000
1 Replies

6. Shell Programming and Scripting

Read and write to tcp socket

Hello all, I have a requirement to read and write to a tcp socket from an HP-UX shell script. I see a /dev/tcp character device on my servers: crw-rw-rw- 1 root root 72 0x00004f Mar 28 18:37 /dev/tcp So I believe this is what I should use. The problem is that all the... (2 Replies)
Discussion started by: lupin..the..3rd
2 Replies

7. IP Networking

Tcp ip port open but no such process (merged: Release A Port)

i want to kill a tcp connection by killing its pid with netstat -an i got the tcp ip connection on port 5914 but when i type ps -a or ps-e there is not such process running on port 5914 is it possible that because i do not log on with proper user account i can not see that process running? (30 Replies)
Discussion started by: alinamadchian
30 Replies

8. Solaris

Tcp Socket (Connection refused) to my server box

I installed a fresh copy of Solaris 7 and present up my ip and domain for my web services but when I try to connect to it I get the following error; TCPActiveOpen: connect failed tcp/192.168.1.148/7900: 146 (Connection refused). the port is open in my router but I don't no were to add it in... (5 Replies)
Discussion started by: Wpgn
5 Replies

9. Solaris

Installing TCP-MUX protocol socket help required.

I been looking for a good guide or some help on how to install and setup TCP-MUX protocol socket on my Solaris 7 servers. Can anyone point me in right direction of help me ? Thanks (5 Replies)
Discussion started by: Wpgn
5 Replies

10. AIX

How to repair a TCP/IP socket in state: CLOSE_WAIT?

Hi The clients connect to my server -using port 9130. But no client could connect to my server at this time. I've checked already and this is the result netstat -Aan|grep -v 127.0.0.1|grep 9130|pg f10006000abcb398 tcp4 10313 0 10.0.89.81.9130 10.158.70.24.1705 CLOSE_WAIT... (8 Replies)
Discussion started by: bobochacha29
8 Replies
poll(2) 							   System Calls 							   poll(2)

NAME
poll - input/output multiplexing SYNOPSIS
#include <poll.h> int poll(struct pollfd fds[], nfds_t nfds, int timeout); DESCRIPTION
The poll() function provides applications with a mechanism for multiplexing input/output over a set of file descriptors. For each member of the array pointed to by fds, poll() examines the given file descriptor for the event(s) specified in events. The number of pollfd struc- tures in the fds array is specified by nfds. The poll() function identifies those file descriptors on which an application can read or write data, or on which certain events have occurred. The fds argument specifies the file descriptors to be examined and the events of interest for each file descriptor. It is a pointer to an array with one member for each open file descriptor of interest. The array's members are pollfd structures, which contain the following members: int fd; /* file descriptor */ short events; /* requested events */ short revents; /* returned events */ The fd member specifies an open file descriptor and the events and revents members are bitmasks constructed by a logical OR operation of any combination of the following event flags: POLLIN Data other than high priority data may be read without blocking. For STREAMS, this flag is set in revents even if the mes- sage is of zero length. POLLRDNORM Normal data (priority band equals 0) may be read without blocking. For STREAMS, this flag is set in revents even if the message is of zero length. POLLRDBAND Data from a non-zero priority band may be read without blocking. For STREAMS, this flag is set in revents even if the mes- sage is of zero length. POLLPRI High priority data may be received without blocking. For STREAMS, this flag is set in revents even if the message is of zero length. POLLOUT Normal data (priority band equals 0) may be written without blocking. POLLWRNORM The same as POLLOUT. POLLWRBAND Priority data (priority band > 0) may be written. This event only examines bands that have been written to at least once. POLLERR An error has occurred on the device or stream. This flag is only valid in the revents bitmask; it is not used in the events member. POLLHUP A hangup has occurred on the stream. This event and POLLOUT are mutually exclusive; a stream can never be writable if a hangup has occurred. However, this event and POLLIN, POLLRDNORM, POLLRDBAND, or POLLPRI are not mutually exclusive. This flag is only valid in the revents bitmask; it is not used in the events member. POLLNVAL The specified fd value does not belong to an open file. This flag is only valid in the revents member; it is not used in the events member. If the value fd is less than 0, events is ignored and revents is set to 0 in that entry on return from poll(). The results of the poll() query are stored in the revents member in the pollfd structure. Bits are set in the revents bitmask to indicate which of the requested events are true. If none are true, none of the specified bits are set in revents when the poll() call returns. The event flags POLLHUP, POLLERR, and POLLNVAL are always set in revents if the conditions they indicate are true; this occurs even though these flags were not present in events. If none of the defined events have occurred on any selected file descriptor, poll() waits at least timeout milliseconds for an event to occur on any of the selected file descriptors. On a computer where millisecond timing accuracy is not available, timeout is rounded up to the nearest legal value available on that system. If the value timeout is 0, poll() returns immediately. If the value of timeout is -1, poll() blocks until a requested event occurs or until the call is interrupted. The poll() function is not affected by the O_NDELAY and O_NONBLOCK flags. The poll() function supports regular files, terminal and pseudo-terminal devices, STREAMS-based files, FIFOs and pipes. The behavior of poll() on elements of fds that refer to other types of file is unspecified. The poll() function supports sockets. A file descriptor for a socket that is listening for connections will indicate that it is ready for reading, once connections are avail- able. A file descriptor for a socket that is connecting asynchronously will indicate that it is ready for writing, once a connection has been established. Regular files always poll() TRUE for reading and writing. RETURN VALUES
Upon successful completion, a non-negative value is returned. A positive value indicates the total number of file descriptors that has been selected (that is, file descriptors for which the revents member is non-zero). A value of 0 indicates that the call timed out and no file descriptors have been selected. Upon failure, -1 is returned and errno is set to indicate the error. ERRORS
The poll() function will fail if: EAGAIN Allocation of internal data structures failed, but the request may be attempted again. EFAULT Some argument points to an illegal address. EINTR A signal was caught during the poll() function. EINVAL The argument nfds is greater than {OPEN_MAX}, or one of the fd members refers to a STREAM or multiplexer that is linked (directly or indirectly) downstream from a multiplexer. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ SEE ALSO
intro(2), getmsg(2), getrlimit(2), putmsg(2), read(2), write(2), select(3C), attributes(5), standards(5), chpoll(9E) STREAMS Programming Guide NOTES
Non-STREAMS drivers use chpoll(9E) to implement poll() on these devices. SunOS 5.10 23 Aug 2001 poll(2)
All times are GMT -4. The time now is 09:49 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy