Advertisement

Changing SOCKADDR_IN Real-Time :: Winsock

Started by May 11, 2002 12:26 PM
6 comments, last by kuphryn 22 years, 9 months ago
Hi. I am working on a simple message program via IP and TCP protocols. Right now, I ininitialize a listening socket on a default (I set at 21). The user can change the port anytime; however, the actual SOCKADDR_IN that was binded with the listening socket is working on the default port. Is there a way to modify the port in a SOCKADDR_IN in real-time? Do I have to close the listening sock and bind it with a new SOCKADDR_IN with the new port? Thanks, Kuphryn P.S. From my experience with ftp servers and clients, I believe you have to "reconnect." Thus I believe you have to close whatever socket and start over. I would like to make sure that is true.
AFAIK, you''ll have to close, rebind and reopen your listening socket.

As you know, port 21 is reserved for ftp, and only applications witb root privileges can open ports numbers below 1024.

I''d pick another port if I were you.

[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]

Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
Okay. Thanks.

have one last question.

Let say you create a listening sock at port 21. Okay. Now let say a user logs on and everything works well. Now, let say you go and change the server port. Afterward, you disconnect the server (close it). Uponing disconnecting the server I assume you close the both the listening socket and the accept socket (disconnect the user as well). Is it possible to reuse both the listening sock and accept socket? In other words, once you call closesocket(...) on one or more sockets, is it possible to reuse them, i.e:

// listeningsock(used) = WSASocket(...);
// bind(listeningsock...);
// listen(...);

Kuphryn
I don''t see why not. I assume WSASocket means the same as socket and so will return a SOCKET type, which as has already been pointed out is an int, therefore the assignment will simply change the value of listeningsock. That is of course unless I''ve misinterpreted

// listeningsock(used) = WSASocket(...);

I see this as
SOCKET listeningsock;listeningsock = WSASocket(...); 


If this isn''t what you mean, feel free to clarify.
You can only reuse sockets on Windows XP or higher. Look up the DisconnectEx API function. I''m not sure about Linux but under Windows 9x/Me/NT/2000 you have to reaquire a socket descriptor by calling socket() or WSASocket.





Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
Thanks.

JuNC. That is basically what I was thinking. Except, I will not redeclare.

// listeningsock = WSASocket(...);

Kuphryn
Advertisement
Ok good, just to clarify: I wasn''t implying you redeclare the variable I was just pointing out that putting

//listeningsock(used) = WSASocket(...)

is slightly misleading in that it looks like an assignment of a funtion. I just showed how I was interpreting that.

quote:

You can only reuse sockets on Windows XP or higher. Look up the DisconnectEx API function. I''m not sure about Linux but under Windows 9x/Me/NT/2000 you have to reaquire a socket descriptor by calling socket() or WSASocket.



Sounds dangerous to me to reuse a socket to connect to some other place, in my mind the semantics of a socket work the same as a file descriptor (as the do in UNIX) so they should point to one place.. (except that falls down if ur using datagram based or broadcasting so I''ll shut up now )
Yeah.

I think Dire.Wolf was thinking about my treating one socket as multiple sockets for multiple connections.

Kuphryn

This topic is closed to new replies.

Advertisement