473,386 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
0 1442

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ali | last post by:
I'm trying to write a really basic chat program that allows 2 client programs to send messages to each other via a server. I've managed to write the code so that both clients can connect to the...
1
by: Dmitry Akselrod | last post by:
Hello everyone, I have a vb.net application that wraps the TCPListener object in a class. The server connects to the local interface and establishes itself on port 9900. It then polls for...
0
by: richard | last post by:
OS: Winxp and Win2003 Visual Basic.NET 2003 MS-SQL Server 2000 hey all I am a newbie in vb.net but i have managed to build a simple chat server in vb.net using socket and a client connecting...
1
by: Thomee Wright | last post by:
I'm having a problem with a pair of applications I'm developing. I have a server application which interacts with several instruments, and a client app which connects to the server and provides a...
0
by: Suresh | last post by:
Hi Guys I have Db2 server installed on remote server. i am connecting to that remote server by using VPN. I want to connect that remote DB2 server instance using my local machine DB2...
5
by: Suresh | last post by:
Hi Guys I have Db2 server installed on remote server. i am connecting to that remote server by using VPN. I want to connect that remote DB2 server instance using my local machine DB2...
0
by: NoaGross | last post by:
Hi, I'm relly new in java and I have a problem. I'm using java applet. When using http all ok, but when trying to use https i get: Java Plug-in 1.5.0_10 Using JRE version 1.5.0_10 Java...
9
by: darthghandi | last post by:
I am trying to create a server application using asynchronous sockets. I run into a problem when I try to connect to my server using a non-.net program. I can establish the connection, and send...
10
by: mairhtin o'feannag | last post by:
Hello, I'm having problems connecting to my new v9 db box. The pertinent information is below: DB2_db2inst1 60000/tcp DB2_db2inst1_1 60001/tcp DB2_db2inst1_2 60002/tcp DB2_db2inst1_END...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.