Click here to Skip to main content
15,920,896 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Name of the '|' key ? Pin
David Crow16-Oct-03 6:52
David Crow16-Oct-03 6:52 
GeneralRe: Name of the '|' key ? Pin
Anonymous16-Oct-03 7:51
Anonymous16-Oct-03 7:51 
AnswerRe: Name of the '|' key ? Pin
RChin17-Oct-03 6:34
RChin17-Oct-03 6:34 
GeneralVK_TAB in an edit control in a dialog Pin
Dave_16-Oct-03 5:03
Dave_16-Oct-03 5:03 
GeneralRe: VK_TAB in an edit control in a dialog Pin
igor196016-Oct-03 7:50
igor196016-Oct-03 7:50 
GeneralRe: VK_TAB in an edit control in a dialog Pin
Dave_16-Oct-03 8:02
Dave_16-Oct-03 8:02 
GeneralSocket Programming : connecting to server Pin
Cyberizen16-Oct-03 5:02
Cyberizen16-Oct-03 5:02 
GeneralRe: Socket Programming : connecting to server Pin
script_smiths16-Oct-03 22:23
script_smiths16-Oct-03 22:23 
I'm not 100% familiar with CAsyncSocket (this is what you are using?), but this tips might help/be applicable, but do take it with a pinch of salt!!

1. I don't think you have to "Listen" repeatedly, just call Listen() once
2. You need to Accept() connections in you server, implement the OnAccept() notification
3. You shouldn't bind you client socket to a specific port, rather use Create() and let the socket pick the best port for the client
4. Don't use ports in the 0-1024 range, these are resevered, rather us a number > 7000, this is what I usually do.

So maybe:

class CMySocket : public CAsyncSocket
{
CSocket m_Client;
protected:
virtual void OnAccept(int)
{
Accept(m_Client);
}
}

...
//in MainFrame
CMySocket server;
...

int MainFrame ::OnCreate(LPCREATESTRUCT lpcs)
{
if ( !AfxSocketInit() )
{
MessageBox("Socket init error");
}

//create the server first
if ( server.Create(7000,SOCK_STREAM) )
{
server.Listen();
MessageBox("Server Socket Created");
}

return CWnd::OnCreate(lpcs);
}


>>now the second part that is the CLIENT has the following code

int MainFrame ::OnCreate(LPCREATESTRUCT lpcs)
{
if ( !AfxSocketInit() )
{
MessageBox("Socket init error");
}

if ( client.Create() )
{
MessageBox("Client Socket Created");
}

return CWnd::OnCreate(lpcs);
}

// OnConnect is a method delacared in your mainframe
// and called by you in some way, maybe in response to
// a menu item, etc DONT FORGET TO CALL THIS METHOD!!!
void MainFrame ::OnConnect()
{
// this is nasty!! there should be a delay of some kind
// or perhaps some other mechansim, if the connection fails
// this will just flood the poor server!!
// what if it never succeeds? You app will just lock up and the
// MMI will cease to respond!

while ( client.Connect("172.16.64.89",7000) != TRUE )
{
client.Connect("172.16.64.89",7000);
}

MessageBox("Connected to Server");
}




Windows, Linux and Internet Development Consultant
Email: corporate@scriptsmiths.com
Web: http://www.scriptsmiths.com
GeneralPrinting Landscape on Windows 2000 Pin
Jonathan Craig16-Oct-03 5:00
Jonathan Craig16-Oct-03 5:00 
GeneralRe: Printing Landscape on Windows 2000 Pin
Steve S16-Oct-03 5:26
Steve S16-Oct-03 5:26 
GeneralDaylight saving problems when using winapi's timezone functions. Pin
Patric_J16-Oct-03 4:56
Patric_J16-Oct-03 4:56 
GeneralRe: Daylight saving problems when using winapi's timezone functions. Pin
Patric_J20-Oct-03 7:40
Patric_J20-Oct-03 7:40 
GeneralCShellFileOp Pin
Kevin Marren16-Oct-03 3:53
Kevin Marren16-Oct-03 3:53 
GeneralRe: CShellFileOp Pin
David Crow16-Oct-03 6:54
David Crow16-Oct-03 6:54 
GeneralFILE Pin
hph16-Oct-03 3:51
hph16-Oct-03 3:51 
GeneralRe: FILE Pin
David Crow16-Oct-03 6:57
David Crow16-Oct-03 6:57 
GeneralSoftware analysis tools Pin
Shay Harel16-Oct-03 3:42
Shay Harel16-Oct-03 3:42 
GeneralClistBox problem here........help!!!!! :-( Pin
Steve Obbayi16-Oct-03 3:31
professionalSteve Obbayi16-Oct-03 3:31 
GeneralRe: ClistBox problem here........help!!!!! :-( Pin
Rafael Fernández López16-Oct-03 9:39
Rafael Fernández López16-Oct-03 9:39 
GeneralRe: ClistBox problem here........help!!!!! :-( Pin
Mike Danberg16-Oct-03 10:01
Mike Danberg16-Oct-03 10:01 
GeneralRe: ClistBox problem here........help!!!!! :-( Pin
Steve Obbayi16-Oct-03 19:07
professionalSteve Obbayi16-Oct-03 19:07 
GeneralCComboBox Pin
De Nardis Andrea16-Oct-03 3:29
De Nardis Andrea16-Oct-03 3:29 
GeneralRe: CComboBox Pin
David Crow16-Oct-03 7:07
David Crow16-Oct-03 7:07 
GeneralRe: CComboBox Pin
De Nardis Andrea16-Oct-03 8:04
De Nardis Andrea16-Oct-03 8:04 
Generalbluetooth Pin
da_adel16-Oct-03 3:28
da_adel16-Oct-03 3:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.