Sockets FAQ/Tutorial

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
jdubchak
In need of some credit
In need of some credit
Posts: 9
Joined: Wed Sep 01, 2004 11:45 am
Location: Minneapolis, MN
Contact:

Postby jdubchak » Fri Jun 24, 2005 2:21 pm

Regarding the following snippet of code:

Code: Select all

wxMyServerThread* pThread =
new wxMyServerThread(this, pSocket);
pThread->Create();
pThread->Run();


Who's responsibility is it to delete the pointer to prevent a memory leak?

Thanks,
John

User avatar
Ryan Norton
Moderator
Moderator
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Postby Ryan Norton » Fri Jun 24, 2005 7:20 pm

jdubchak wrote:Who's responsibility is it to delete the pointer to prevent a memory leak?


As my comment mentions wx itself does so when the thread terminates (is there I way I can phrase it better perhaps?).
[Mostly retired moderator, still check in to clean up some stuff]

Almeida
In need of some credit
In need of some credit
Posts: 1
Joined: Fri Sep 16, 2005 1:39 pm
Location: Brazil, MG

Client IP address in server

Postby Almeida » Fri Sep 16, 2005 2:30 pm

How to get the client IP address in server? In Windows API the server get the client address in accept():

Code: Select all


    SOCKET sd_listen, sd_accept;
    struct sockaddr_in addr_srv, addr_client;
    int size_addr, error;

    size_addr = sizeof(struct sockaddr_in);

    (...)

    error = listen(sd_listen, SOMAXCONN);

    (...)

    // Acceptting connection and getting client address.
    sd_accept = accept(sd_listen, (struct sockaddr*)&addr_client, &size_addr);

    // Printting client address.
    printf ("Accepted connection from client at %s.\n", inet_ntoa(addr_client.sin_addr));   
   


And in wxWindows? How to do it?

Almeida

upCASE
Site Admin
Site Admin
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Postby upCASE » Fri Sep 16, 2005 2:46 pm

Hi!
Use wxSocketBase::GetPeer();

Code: Select all

wxIPV4address adr;
server->GetPeer(adr);
wxString ip = adr.Hostname();
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda

GotenXiao
In need of some credit
In need of some credit
Posts: 8
Joined: Tue Oct 11, 2005 8:40 pm

Postby GotenXiao » Tue Nov 29, 2005 10:44 pm

One thing that is currently bugging me is how to do a broadcast query...

For example, sending a few packets out on the broadcast address and asking for any servers. Said servers are listening and grab said packets and reply so the client knows where to go (similar, if not identical, to a server browser for, say, FEAR).

Ideally I'd like to keep it in wx calls, since resorting to raw WinSock calls would more than somewhat suck :/

[EDIT]Found this thread (http://forums.wxwidgets.org/viewtopic.php?t=5009), but it's not entirely clear on the code side. Anyone care to clarify a bit more, I'm struggling a bit :/
Image

upCASE
Site Admin
Site Admin
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Postby upCASE » Wed Nov 30, 2005 9:46 am

Hi!
Please start a new thread for that, as this thread is a FAQ and you should not post questions to it.

In the thread, refer to my code and specify what exactly troubles you :)
If I can, I'll comment on that.
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda

Peer Sommerlund
Knows some wx things
Knows some wx things
Posts: 43
Joined: Tue Jun 13, 2006 7:21 am
Location: Denmark
Contact:

Postby Peer Sommerlund » Thu Jul 13, 2006 12:52 pm

May I suggest this FAQ is merged into http://www.wxwidgets.org/wiki/index.php/WxSocket

This will have two bennefits:
1) Less confusion because there is only one source for wxSocket info
2) Faster to read because you don't have to go through the entire thread to make sure you did not miss some important points.

Well, with respect to 1) make that "one less source for wxSocket info" :)

fronda1
In need of some credit
In need of some credit
Posts: 3
Joined: Wed Aug 30, 2006 11:35 am

download?

Postby fronda1 » Wed Aug 30, 2006 11:46 am

where is example to download?

upCASE
Site Admin
Site Admin
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Re: download?

Postby upCASE » Wed Aug 30, 2006 11:54 am

fronda1 wrote:where is example to download?

???
Example for what? wxSocket classes? Check the samples dir of the wxWidgets sources.
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda

morya
Experienced Solver
Experienced Solver
Posts: 96
Joined: Fri Dec 14, 2007 2:29 am
Location: Xuzhou, China
Contact:

Postby morya » Thu Apr 17, 2008 1:39 am

Thx for the example.
mark here
so I am able to track this thread.

arust
Knows some wx things
Knows some wx things
Posts: 34
Joined: Tue Jan 15, 2008 5:13 am
Location: Beijing, China
Contact:

Postby arust » Mon Mar 23, 2009 3:49 am

How to using a thread for each server socket, like Multi Tab WebBrowser

hats
Experienced Solver
Experienced Solver
Posts: 70
Joined: Wed Sep 16, 2009 3:50 pm
Location: China
Contact:

Postby hats » Thu Dec 30, 2010 1:01 am

upCASE wrote:Hi!
Use wxSocketBase::GetPeer();

Code: Select all

wxIPV4address adr;
server->GetPeer(adr);
wxString ip = adr.Hostname();


Thanks a lot! I also have the same question.
wx2.8.11
Win XP GCC 4.4.1/VC2005
Ubuntu 10.04 GCC 4.4.3

onionman
Knows some wx things
Knows some wx things
Posts: 34
Joined: Wed May 20, 2009 7:03 pm

Re: Sockets FAQ/Tutorial

Postby onionman » Tue Dec 13, 2011 10:44 pm

I find that my game just freezes unless I use the SOCKET_NOWAIT flag. Not sure what that's about, but this post would have really helped me out back when, thank you!

There just aren't any good tutorials out there on using wxSockets with multiple clients.

Lord Asriel
In need of some credit
In need of some credit
Posts: 6
Joined: Thu Nov 17, 2011 1:28 am

Re: Sockets FAQ/Tutorial

Postby Lord Asriel » Fri Dec 16, 2011 8:39 pm

This might be a good FAQ, but it is in no way a tutorial. It is meant to take people that know at least 40% of the subject to higher knowledge areas. However, people with 1% like me stand no chance. So, could you plase elaborate more OR point me to the right direction?

I tried one of the links suggested on the end of the first posts, but after getting some (interesting and usuful indeed) information, it started demonstrating the use of the winsock library functions, which I do not need to learn since I'm going to use wxSocket, which is a different thing, right?

Just to understand sockets, I am planning to make a chat program, the simplest thing I can think of: one input and one output text control, a send button, a create server button, and a join server button that spawns a dialog to set the ip and port. Am I thinking right? Please give me as much info as you can! Ready code would be appreciated (hope that doesn't present me as student-requesting-homework...)

sapthagiri
Earned a small fee
Earned a small fee
Posts: 23
Joined: Mon Jan 09, 2017 12:36 pm

Re: Sockets FAQ/Tutorial

Postby sapthagiri » Thu Aug 10, 2017 4:51 am

Hi Team,
I was just reading the posts related to wxsockets flag : http://docs.wxwidgets.org/3.1.0/socket_ ... ocketFlags

Observed that in some of the sample applications during socket creation or before socket send/recv() operations, they are setting wxsocket flags, please find the following enumerator:
wxSOCKET_NONE -> Normal functionality.
wxSOCKET_NOWAIT -> Read/write as much data as possible and return immediately.
wxSOCKET_WAITALL -> Wait for all required data to be read/written unless an error occurs.
wxSOCKET_BLOCK -> Block the GUI (do not yield) while reading/writing data.
wxSOCKET_REUSEADDR -> Allows the use of an in-use port.
wxSOCKET_BROADCAST -> Switches the socket to broadcast mode.
wxSOCKET_NOBIND -> Stops the socket from being bound to a specific adapter (normally used in conjunction with wxSOCKET_BROADCAST)
wxSOCKET_NOWAIT_READ -> Read as much data as possible and return immediately.
wxSOCKET_WAITALL_READ -> Wait for all required data to be read unless an error occurs.
wxSOCKET_NOWAIT_WRITE -> Write as much data as possible and return immediately.
wxSOCKET_WAITALL_WRITE -> Wait for all required data to be written unless an error occurs.

As per understanding the above flags are customized for wxWidgets platform or environment.
My query is do we have similar socket flags available in the Linux or Unix platform.
Browsed few forums & found that :
setsockopt(): passing flags such as SO_REUSEADDR or SO_BROADCAST etc..

Thanks & Regards,
Giri


Return to “C++ Development”

Who is online

Users browsing this forum: No registered users and 16 guests