Alex Rivera | Logout

How to Create a TCP socket connect using C to a predefined port

Asked 2008-10-15T09:37:37.207
11

I have a very simple question. I want to test whether a particular port is currently under use or not. For this, I want to bind a TCP socket to the port, if the connection is refused means the port is in use and if not that mean the port is free.

Can someone please tell me how can I write the TCP socket code in C? I am on a solaris platform.

I know its very basic. But I appreciate your help. Thanks in advance.

Edit
Report

2 Answers

-1

Do you just want to test if the particular port is currently in use? (and don't really need to make a program). If so, you can use telnet:

telnet host port

If the connection fails, it's not in use. If it connects and waits for input from you, it's in use :)

answered 2008-10-15T10:14:01.423
-2

You might want to look at the source code of netstat. I believe there is a netstat in Solaris as well.

answered 2008-10-15T10:06:27.367

Your Answer