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
Hi

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.

Regards

Usman
////////////////////////////////////////////////////////////////////////////
/////////////////////////
int nServerPort = 0;
try
{

// Create the listening socket...
m_mainSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);

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);

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

for(;;)
{
Socket socketForClient=tcpListener.AcceptSocket();
if(socketForClient.Connected)
{
Console.WriteLine("Client connected");
}
}
*/

}
catch(SocketException se)
{
//Exception
}
return nServerPort;

////////////////////////////////////////////////////////////////////////////
/////////////////
OnClientConnect Function
////////////////////////////////////////////////////////////////////////////
/////////////////

public void OnClientConnect(IAsyncResult asyn)
{
try
{
// 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
client

//Close the socket
workerSocket.Shutdown(SocketShutdown.Both);
workerSocket.Close();

//Transfer is complete. close the server socket
m_mainSocket.Close();
}
catch(ObjectDisposedException)
{
//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.