Visit The New, Modern Unix Linux Community

Read and write to tcp socket

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read and write to tcp socket
# 1  
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 examples I find online are using /dev/tcp as a directory, not as a file. See here for an example:

Does anyone know how I interact with /dev/tcp as a character device? How do I pass the IP address and port number to it?

Thanks all

Last edited by lupin..the..3rd; 06-21-2012 at 09:14 PM..
# 2  
You should have a look at the nc command.
# 3  

usage="${0##*/} SERVER USER PASSWORD [PORT]"

case $1 in
    -*|"") printf "USAGE: %s\n" "$usage"; exit ;;


CR=$'\r'                            ## carriage return; for removal of
exec 3<>/dev/tcp/$host/$port        ## connect to POP3 server, port 110
read ok line <&3                    ## get response from server
[ "${ok%$CR}" != "+OK" ] && exit 5  ## check that it succeeded
echo user "$user" >&3               ## send user name
read ok line <&3                    ## get response
[ "${ok%$CR}" != "+OK" ] && exit 5  ## check that it succeeded
echo pass "$passwd" >&3             ## send password
read ok line <&3                    ## get response
[ "${ok%$CR}" != "+OK" ] && exit 5  ## check that it succeeded
echo stat >&3                       ## request number of messages
read ok num x <&3                   ## get response
echo Messages: $num                 ## display number of messages
echo quit >&3                       ## close connection

These 2 Users Gave Thanks to cfajohnson For This Post:

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #671
Difficulty: Medium
DOS primarily consists of Microsoft's MS-DOS and a rebranded IBM version under the name PC DOS, both of which were introduced in 1981.
True or False?

10 More Discussions You Might Find Interesting

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

2. Programming

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... (2 Replies)
Discussion started by: tjones1105
2 Replies

3. Programming

Need help about read() and write() on TCP/IP

HI I need to implement a client/server TCP application. the customer is the client and the bartender is the server. When the customer enter the Bar, client connects to the server Server should reply the client immediately. Other wise if the server is busy, it should send an update message... (10 Replies)
Discussion started by: lixiao1212
10 Replies

4. Programming

socket function to read a webpage (socket.h)

Why does this socket function only read the first 1440 chars of the stream. Why not the whole stream ? I checked it with gdm and valgrind and everything seems correct... #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> #include... (3 Replies)
Discussion started by: cyler
3 Replies

5. IP Networking

read/write,write/write lock with smbclient fails

Hi, We have smb client running on two of the linux boxes and smb server on another linux system. During a backup operation which uses smb, read of a file was allowed while write to the same file was going on.Also simultaneous writes to the same file were allowed.Following are the settings in the... (1 Reply)
Discussion started by: swatidas11
1 Replies

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

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

8. Programming

read/write socket error

I have client and server connected. client write and read from csock. server write and read from ssock suppose the server does : .... close(ssock); //send FIN to client othertask(); .... READ ERROR if after the server close() the client does: ... read(csock,...); ...... (2 Replies)
Discussion started by: gio
2 Replies

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

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

Featured Tech Videos