open a closed tcp port in windows by using C++ - ports

anyone know how to open a closed port using C++. for example :
if i check port 6091 if it's opened then it's alright but
if it's closed, the program will open that port forcely.
i mean , is it possible to do it ?
thank you. i appreciate any help.

Related

How to let a socket server accept only once [duplicate]

This question already has answers here:
How can I refuse a socket connection in C?
(5 answers)
Closed 4 years ago.
I have a really big headache.
If there is a case that I have many socket client and only one socket server, how can I make the server only accept the first connect() order he received, and fail all the other connect() from other client? I'm using C in linux btw
I'm really struggle with this question. In other words, how can I make a client recognizance that "Hey,this port number of this ip is busy, I need to try another one maybe."
My teacher told me that I will get a timeout error if i just let a client connect to a server, while another client has connect to the same ip same port recently. But, well, I just cannot find where is the error. Both of the connect() functions in two clients return 0.
Maybe I misunderstand my teacher's word,but since the program doesn't crash and no negative number returned from functions, where is it,how can I find it?
Thanks a lot.
Try using a value of 0 or 1 for backlog in your listen() call.
I haven't tried it, but the man page claims that extra connections will get a ECONNREFUSED error.
Immediately after calling accept to get your actual socket to the first client close the listener. Otherwise another client can queue up.

How to write a function to disconnect client from server in C? [duplicate]

This question already has answers here:
close vs shutdown socket?
(9 answers)
Closed 7 years ago.
I tried server client tcp code in c, I want to add some functionality to disconnect client from server. I search on google for it. I found function shutdown(), I am not getting idea how to do it ?
To disconnect a client from the server, just close the fd linked to this client.
To disconnect a client to a server from the client, just close the client socket.
This is a quick way to disconnect, but don't forget to send a last exit message when you are leaving from a server / disconnecting a client.
No need of shutdown here. (Except if you share fd between processes, but without more information we cannot be more precise)

How do I remove a CLOSE_WAIT socket connection?

I have write a simple program using socket in C that create a connect between X86 running windows and ARM running embedded linux(consist of only Busybox and libc).Suddenly this small program could not connect the windows and linux,then I running "netsta -a" found 3 socket's state is CLOSE_WAIT and PID is NULL.So I try to modify “net.ipv4.tcp_keepalive_×” but because busybox has only the basic functions that I could not using /etc/rc.d/init.d/network restart makes the modify take effect.
So I want to know:
how to make the change take effect with Busybox?
how I using socket can avoid the CLOSE_WAIT problem?
How do I remove a CLOSE_WAIT connection that doesn't belong to any tasks?
As we've established that the process is still running, it does belong to a task. We've also established that the netstat output was a complete red herring.
All you have to do is close the socket. You probably forget to close it after you got the connection failure. It's just a common or garden file/socket descriptor leak.
You might want to check out: https://github.com/rghose/kill-close-wait-connections
What this script does is send out the ACK which the connection was waiting for.
This is what worked for me.

How can I use a custom port (not default) for connecting a server and client using only telnet or FTP (in socket programming in C)?

I read the link on Beej's socket programming tutorial which was really good. Although it didn't answer a question i was pondering since somehwere in the beginning of the tutorial and I'm not able to find the solution to this on Stackoverflow or google.
Since my question may seem ambiguous in the title, let me elaborate it so that it's more lucid.
I just want to be able to use any port (> 1024 of course) say 12345 and establish a TELNET connection or an FTP connection but not using the default ports (23, 21, etc).
I have used the getaddrinfo function and used the AF_INET macro for IPV4, SOCK_STREAM for TCP and I could use "telnet" or port 23 for telnet in this function. But I want to know how do I (or can I use a custom port) use a custom port for the same. I have no problem with the code given in the tutorial link. I am new to this. So I'm just hoping that I can get some clarity with this.
PS: I am also reading Unix Network Programming (vol 1) although I just started and I can't seem to find it in that either.
Many thanks!
Then writing your own TCP client/server applications you are free to use any ports you like (aside the fact that you need root privileges to bind ports below 1024) and which are not used by the rest of the system. So you should bind to desired port/interface on server side and use the same port for client application.
Just bind your socket to that port number before calling listen().
probably you can enter entry based one new port in services file located in system32\etc\drivers

/proc/net/tcp Alternative Under Solaris 11

On Most Linux Distributions, i was able to list all tcp connections by reading /proc/net/tcp, but this Doesn't exists on solaris, is there a file that i can read tcp connections from on Solaris 11?
thanks.
EDIT: forgot to mention that i'm coding in c.
If you're trying to rewrite netstat, I suggest looking at the source code for it: https://hg.java.net/hg/solaris~on-src/file/tip/usr/src/cmd/cmd-inet/usr.bin/netstat/netstat.c
The important parts are mibopen, which opens /dev/arp and pushes the tcp STREAMS module onto it, and mibget which actually requests the connection information. The code is a bit complicated, so I suggest stepping through the code in a debugger to understand how it works. The key syscalls are open, ioctl, putmsg, and getmsg.
If you just want to see what sockets a process has open, you can inspect /proc/PID/fd, as in pfiles: https://hg.java.net/hg/solaris~on-src/file/tip/usr/src/cmd/ptools/pfiles/pfiles.c
You should either use netstat -an or pcp

Resources