By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,361 Members | 1,824 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,361 IT Pros & Developers. It's quick & easy.

Problem connecting to a Server in C# from client in VC++

P: n/a

I'm having problem with a scenarion where I have a server written in C# and
client written in VC6++.

Here is the server code that i'm using including the Callback function for
handling clients. Also there's a commented code where i'm using TCPListener
instead of simple Socket Class. The problem i'm having is that when I run
this code using Socket class, the server starts well but "OnClientConnect"
method never gets called. On debugging I found out that on the (VC++) client
side the socket connects successfully and even sends data successfully on
this connected socket. But on this C# server side the OnClientConnect
method never gets called. I dont know who's accepting the client.

On the contrary when I comment out this Socket code and uncomment the
TCPListener code, then the code gets executed as soon as the VC6 client
executes its "connect" call. Can someone please tell where could the problem
be with using Socket class in this server code, I'm really stuck in it.


int nServerPort = 0;

// Create the listening socket...
m_mainSocket = new Socket(AddressFamily.InterNetwork,

IPHostEntry hostEntry = Dns.Resolve(szIPAddress);
IPAddress ipAddress = hostEntry.AddressList[0];

IPEndPoint ipLocal = new IPEndPoint (ipAddress, nServerPort);
// Bind to local IP Address...
m_mainSocket.Bind( ipLocal );

//IP Endpoint must be populated. Get the port
ipLocal = (IPEndPoint) m_mainSocket.LocalEndPoint;

nServerPort = ipLocal.Port;

// Start listening...
m_mainSocket.Listen (4);
// Create the call back for any client connections...
m_mainSocket.BeginAccept(new AsyncCallback (OnClientConnect), null);

IPHostEntry hostEntry = Dns.Resolve(szIPAddress);
IPAddress ipAddress = hostEntry.AddressList[0];
TcpListener tcpListener = new TcpListener(ipAddress, 33333);

Socket socketForClient=tcpListener.AcceptSocket();
Console.WriteLine("Client connected");

catch(SocketException se)
return nServerPort;

OnClientConnect Function

public void OnClientConnect(IAsyncResult asyn)
// Here we complete/end the BeginAccept() asynchronous call
// by calling EndAccept() - which returns the reference to
// a new Socket object
Socket workerSocket = m_mainSocket.EndAccept (asyn);

// Let the worker Socket do the further processing for the connected

//Close the socket

//Transfer is complete. close the server socket
//System.Diagnostics.Debugger.Log(0,"1","\n OnClientConnection: Socket
has been closed\n");
catch(SocketException se)
//MessageBox.Show ( se.Message );

Nov 23 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.