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) 						      BSD System Calls Manual							   POLL(2)

NAME
poll -- synchronous I/O multiplexing LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <poll.h> int poll(struct pollfd fds[], nfds_t nfds, int timeout); int ppoll(struct pollfd fds[], nfds_t nfds, const struct timespec * restrict timeout, const sigset_t * restrict newsigmask); DESCRIPTION
The poll() system call examines a set of file descriptors to see if some of them are ready for I/O. The fds argument is a pointer to an array of pollfd structures as defined in <poll.h> (shown below). The nfds argument determines the size of the fds array. struct pollfd { int fd; /* file descriptor */ short events; /* events to look for */ short revents; /* events returned */ }; The fields of struct pollfd are as follows: fd File descriptor to poll. If fd is equal to -1 then revents is cleared (set to zero), and that pollfd is not checked. events Events to poll for. (See below.) revents Events which may occur. (See below.) The event bitmasks in events and revents have the following bits: POLLIN Data other than high priority data may be read without blocking. POLLRDNORM Normal data may be read without blocking. POLLRDBAND Data with a non-zero priority may be read without blocking. POLLPRI High priority data may be read without blocking. POLLOUT POLLWRNORM Normal data may be written without blocking. POLLWRBAND Data with a non-zero priority may be written without blocking. POLLERR An exceptional condition has occurred on the device or socket. This flag is always checked, even if not present in the events bitmask. POLLHUP The device or socket has been disconnected. This flag is always checked, even if not present in the events bitmask. Note that POLLHUP and POLLOUT should never be present in the revents bitmask at the same time. POLLNVAL The file descriptor is not open. This flag is always checked, even if not present in the events bitmask. If timeout is neither zero nor INFTIM (-1), it specifies a maximum interval to wait for any file descriptor to become ready, in milliseconds. If timeout is INFTIM (-1), the poll blocks indefinitely. If timeout is zero, then poll() will return without blocking. The ppoll() system call, unlike poll(), is used to safely wait until either a set of file descriptors becomes ready or until a signal is caught. The fds and nfds arguments are identical to the analogous arguments of poll(). The timeout argument in ppoll() points to a const struct timespec which is defined in <sys/timespec.h> (shown below) rather than the int timeout used by poll(). A null pointer may be passed to indicate that ppoll() should wait indefinitely. Finally, newsigmask specifies a signal mask which is set while waiting for input. When ppoll() returns, the original signal mask is restored. struct timespec { time_t tv_sec; /* seconds */ long tv_nsec; /* and nanoseconds */ }; RETURN VALUES
The poll() system call returns the number of descriptors that are ready for I/O, or -1 if an error occurred. If the time limit expires, poll() returns 0. If poll() returns with an error, including one due to an interrupted system call, the fds array will be unmodified. COMPATIBILITY
This implementation differs from the historical one in that a given file descriptor may not cause poll() to return with an error. In cases where this would have happened in the historical implementation (e.g. trying to poll a revoke(2)ed descriptor), this implementation instead copies the events bitmask to the revents bitmask. Attempting to perform I/O on this descriptor will then return an error. This behaviour is believed to be more useful. ERRORS
An error return from poll() indicates: [EFAULT] The fds argument points outside the process's allocated address space. [EINTR] A signal was delivered before the time limit expired and before any of the selected events occurred. [EINVAL] The specified time limit is invalid. One of its components is negative or too large. SEE ALSO
accept(2), connect(2), kqueue(2), pselect(2), read(2), recv(2), select(2), send(2), write(2) STANDARDS
The poll() function conforms to IEEE Std 1003.1-2001 (``POSIX.1''). The ppoll() is not specified by POSIX. HISTORY
The poll() function appeared in AT&T System V UNIX. This manual page and the core of the implementation was taken from NetBSD. The ppoll() function first appeared in FreeBSD 11.0 BUGS
The distinction between some of the fields in the events and revents bitmasks is really not useful without STREAMS. The fields are defined for compatibility with existing software. BSD
November 13, 2014 BSD
All times are GMT -4. The time now is 09:02 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy