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
ns_sock(3aolserver)					    AOLserver Built-In Commands 				       ns_sock(3aolserver)

__________________________________________________________________________________________________________________________________________________

NAME
ns_sockaccept, ns_sockblocking, ns_sockcallback, ns_sockcheck, ns_socketpair, ns_socklisten, ns_socklistencallback, ns_socknonblocking, ns_socknread, ns_sockopen, ns_sockselect - Network socket commands SYNOPSIS
ns_sockaccept sockid ns_sockblocking sockid ns_sockcallback sockid script when ns_sockcheck fileid ns_socketpair ns_socklisten address port ns_socklistencallback address port script ns_socknonblocking sockid ns_socknread sockid ns_sockopen ?-nonblock | -timeout seconds? host port ns_sockselect ?-timeout seconds? rfds wfds efds _________________________________________________________________ DESCRIPTION
Performs one of several network socket operations, providing a means to manipulate and use sockets directly within AOLserver. ns_sockaccept sockid Accepts a new connection on the socket specified by sockid and returns a list containing the read and write file descriptors for the socket, respectively. The file descriptors can be used with Tcl's puts, gets, read or any other Tcl socket manipulation commands. ns_sockaccept does not create a socket for you. It is normally used in conjunction with ns_sockcallback, ns_socklisten, and ns_sock- listencallback. You can use ns_sockcheck to see if the socket is valid before using ns_sockaccept on it. ns_sockblocking sockid Sets the socket specified by sockid to blocking mode. In blocking mode, an operation on a socket must complete before the command can return. This can cause your process or thread to block forever if the operation does not complete. If that happens in an ADP, the page may never complete until the socket is interrupted, closed or a TCP timeout occurs. ns_sockcallback sockid script when Registers a socket callback script that will be run when a connection is made to the socket specified by sockid. The socket must already exist. You can create a socket using ns_socklisten. The script should accept sockid and when as its first two arguments. The script is responsible for doing an ns_sockaccept on sockid prior to reading from and writing to the socket. If the same script will handle all when values, you must still register the script four separate times, once for each type of when value. The when value is passed to script when script is invoked. The when value is one of: r - the socket is readable w - the socket is writeable e - the socket has an exceptional condition x - the server is shutting down ns_sockcheck fileid Uses recv() or send() calls to check if a socket is still connected. The fileid is the read or write file id returned by ns_sock- open. This function is useful if you used the -nonblock option with ns_sockopen after calling ns_sockselect. ns_socketpair Creates a pair of connected sockets and returns a list of file ids: the first one for reads and the second one for writes. ns_socklisten address port Creates a new socket and listens for connections at the specified address and port. An asterisk (*) can be used as the address argument to specify all addresses. The socket id is returned. You must use ns_sockaccept to accept connections on the socket. ns_socklistencallback address port script Creates a new socket and listens for connections at the specified address and port. An asterisk (*) can be used as the address argument to specify all addresses. Runs script when a new connection is received. The script must use ns_sockaccept on the ns_socknonblocking sockid Sets the socket specified by sockid to nonblocking. ns_socknread sockid Returns the number of bytes waiting to be read from the socket specified by sockid. ns_sockopen ?-nonblock | -timeout seconds? host port Uses the socket(), bind() and connect() calls to connect to the remote host on the specified port. ns_sockopen returns a list containing the read and write fileids, respectively. The -nonblock option causes ns_sockopen to return immediately, while the connect is still in progress by the operating system. Use ns_sockcheck with the write fileid to see if the connect has completed before attempting to read from or write to the socket. The -timeout option specifies how long to wait for the connect to be made in seconds before timing out. ns_sockselect ?-timeout seconds? rfds wfds efds Uses a call to select() to determine which fileids are readable, writable or have exceptional conditions. ns_sockselect returns a list of three lists: readable fileids, writable fileids, and the fileids with exceptions. The -timeout option specifies how many seconds to wait for ns_sockselect before timing out. The rfds, wfds and efds arguments are lists of readable fileids, writeable fileids, and fileids with exceptions, respectively. Be careful using these socket commands in an ADP or a procedure called by an ADP. If a socket command hangs, the page may not finish being served to the user until the TCP connection times out, typically several minutes later. SEE ALSO
nsd(1), info(n) KEYWORDS
AOLserver 4.0 ns_sock(3aolserver)
All times are GMT -4. The time now is 03:29 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy