Sponsored Content
Top Forums Programming Using socket to test a TCP port Post 302512369 by tjones1105 on Saturday 9th of April 2011 07:23:39 PM
Old 04-09-2011
Using socket to test a TCP port

Hello,
I'm trying to write a small c application to test a tcp port. This works fine for the most part but the default timeout on the connect is very long. I have been reading many posts but and it looks like I need to set the socket to be non-blocking and poll for a result. I have been totally unable to make this work.

Does anyone have a quick example on setting up a timeout on a socket connect?

Thanks,
tom
 

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
STREAM_SOCKET_CLIENT(3) 						 1						   STREAM_SOCKET_CLIENT(3)

stream_socket_client - Open Internet or Unix domain socket connection

SYNOPSIS
resource stream_socket_client (string $remote_socket, [int &$errno], [string &$errstr], [float $timeout = ini_get("default_socket_time- out")], [int $flags = STREAM_CLIENT_CONNECT], [resource $context]) DESCRIPTION
Initiates a stream or datagram connection to the destination specified by $remote_socket. The type of socket created is determined by the transport specified using standard URL formatting: transport://target. For Internet Domain sockets (AF_INET) such as TCP and UDP, the tar- get portion of the $remote_socket parameter should consist of a hostname or IP address followed by a colon and a port number. For Unix domain sockets, the $target portion should point to the socket file on the filesystem. Note The stream will by default be opened in blocking mode. You can switch it to non-blocking mode by using stream_set_blocking(3). PARAMETERS
o $remote_socket - Address to the socket to connect to. o $errno - Will be set to the system level error number if connection fails. o $errstr - Will be set to the system level error message if the connection fails. o $timeout - Number of seconds until the connect() system call should timeout. Note This parameter only applies when not making asynchronous connection attempts. Note To set a timeout for reading/writing data over the socket, use the stream_set_timeout(3), as the $timeout only applies while making connecting the socket. o $flags - Bitmask field which may be set to any combination of connection flags. Currently the select of connection flags is limited to STREAM_CLIENT_CONNECT (default), STREAM_CLIENT_ASYNC_CONNECT and STREAM_CLIENT_PERSISTENT. o $context - A valid context resource created with stream_context_create(3). RETURN VALUES
On success a stream resource is returned which may be used together with the other file functions (such as fgets(3), fgetss(3), fwrite(3), fclose(3), and feof(3)), FALSE on failure. ERRORS
/EXCEPTIONS On failure the $errno and $errstr arguments will be populated with the actual system level error that occurred in the system-level con- nect() call. If the value returned in $errno is 0 and the function returned FALSE, it is an indication that the error occurred before the connect() call. This is most likely due to a problem initializing the socket. Note that the $errno and $errstr arguments will always be passed by reference. EXAMPLES
Example #1 stream_socket_client(3) example <?php $fp = stream_socket_client("tcp://www.example.com:80", $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br /> "; } else { fwrite($fp, "GET / HTTP/1.0 Host: www.example.com Accept: */* "); while (!feof($fp)) { echo fgets($fp, 1024); } fclose($fp); } ?> Example #2 Using UDP connection Retrieving the day and time from the UDP service "daytime" (port 13) on localhost. <?php $fp = stream_socket_client("udp://127.0.0.1:13", $errno, $errstr); if (!$fp) { echo "ERROR: $errno - $errstr<br /> "; } else { fwrite($fp, " "); echo fread($fp, 26); fclose($fp); } ?> NOTES
Warning UDP sockets will sometimes appear to have opened without an error, even if the remote host is unreachable. The error will only become apparent when you read or write data to/from the socket. The reason for this is because UDP is a "connectionless" protocol, which means that the operating system does not try to establish a link for the socket until it actually needs to send or receive data. Note When specifying a numerical IPv6 address (e.g. fe80::1), you must enclose the IP in square brackets--for example, tcp://[fe80::1]:80. Note Depending on the environment, the Unix domain or the optional connect timeout may not be available. A list of available transports can be retrieved using stream_get_transports(3). See "List of Supported Socket Transports" for a list of built in transports. SEE ALSO
stream_socket_server(3), stream_set_blocking(3), stream_set_timeout(3), stream_select(3), fgets(3), fgetss(3), fwrite(3), fclose(3), feof(3), "cURL Functions". PHP Documentation Group STREAM_SOCKET_CLIENT(3)
All times are GMT -4. The time now is 12:19 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy