Sockets

W

WAkthar

I am in the process of converting an MFC client and server application to
C#.

The communication between the client and server was done using simple
WM_COPYDATA. I want to use sockets for the C# version.



Can someone show me how to send and receive data structures using sockets?

A simple client and server example would be much appreciated.



Thanks in advance.
 
C

Carlos J. Quintero [.NET MVP]

You have to use the TcpClient class of the System.Net.Sockets namespace.

Search in Google "TcpClient .NET" to get samples.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 
W

WAkthar

Cheers..but I have searched most places without finding anything to do with
sending strcutures through sockets.

Most examples use for example

NetworkStream serverSockStream = new NetworkStream(serverSocket);
serverStreamWriter = new StreamWriter(serverSockStream);
serverStreamReader = new StreamReader(serverSockStream);

serverStreamWriter.WriteLine("Hi!");
serverStreamWriter.Flush();

This is ok for sending string messages but what about sending and receiving
data structures???
 
S

stork

Buy the Ingo Rammer book about .NET remoting and read it before you
start to code anything.

You don't want to do straight up sockets. You really want to use .NET
remoting. It does all the work for you.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Well all you have to do is convert the struct in a byte[] , then send it as
usual, in the receiving end you must provide a method to convert a byte[]
back to the struct.

You could use the BitConverter class for this. insert two method in your
struct like this:

struct X
{
public byte[] GetBytes() { ... {

public static X FromBytes( byte[] bytes ) { }

}


Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top