networkstream(.NET) vs winsock (Win32)

tiwariajay

Newcomer
Joined
Dec 10, 2003
Messages
2
I have an application (that doesn't support .NET) on which I am using the old Win32 Winsock controls.

I have another application that does support .NET and on which I am using the Socket class and/or TcpClient and TcpListener classes.

My question is: Can I get old Win32 Winsock application to send/read data from/to the .NET application using the Socket class and/or TcpClient and TcpListener classes?

So far I have not been successful no matter who i make the client or the server.

****** code for old win32 winsock app **********
Private Sub Form_Load()
Winsock1.Protocol = sckTCPProtocol
Winsock1.LocalPort = 9100
Winsock1.Listen
End Sub

Private Sub Winsock1_ConnectionRequest _
(ByVal requestID As Long)
' Check if the control State is closed. If not,
' close the connection before accepting the new
' connection.
If Winsock1.State <> sckClosed Then _
Winsock1.Close
' Accept the request with the requestID
' parameter.
Winsock1.Accept requestID
End Sub

Private Sub txtSendData_Change()
Winsock1.SendData txtSendData.Text
End Sub

Private Sub Winsock1_DataArrival _
(ByVal bytesTotal As Long)
Dim strData As String
Winsock1.GetData strData
txtOutput.Text = strData
End Sub

Private Sub Winsock1_Close()
Winsock1.Close
Winsock1.Listen
End Sub



********** code for .NET winsock **********

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

class Client
{
static void Main(string[] args)
{
try
{

TcpClient tcpClient = new TcpClient();

tcpClient.Connect(localIPaddress, 9100);
NetworkStream networkStream = tcpClient.GetStream();

if(networkStream.CanWrite && networkStream.CanRead)
{
// Reads the NetworkStream to a byte buffer.
byte[] bytes = new byte[38];
networkStream.Read(bytes, 0, bytes.Length);

// Returns to the console the data that is received from the host.
string returndata = Encoding.ASCII.GetString(bytes);
Console.Write("This is what the host returned to you: " + returndata.TrimEnd(new char[] {' '}));
Console.ReadLine();
networkStream.Close();
tcpClient.Close();
}
}
catch (Exception e )
{
Console.WriteLine(e.ToString());
}
}
}
 
No errors - the TCP server would have a message "TCP server is up and waiting for client connection......"

However, when I attempt to start the client (which uses the old Win32 winsock controls), I would get a "Address is not available from the local machine" or "Address in use" depending on whether I set the Winsock.LocalPort = 9100 (get "address in use") or the Winsock.RemotePort = 9100 ("address not available"). At no time does the TCPServer recognize the client or vice versa.
 
Why are you setting a localport for the client? Unless it actually listens for any incoming connections, I would leave it at 0 and let Windows assign a port that isn't already used.
 
Back
Top