2

The problem is that my server is written in the old Framework, so I can not use SocketStream there, I use System.Net.Sockets.TcpClient instead. The Client is written in the new framework, where TcpClient and the whole System.Net.Sockets are not supported. In the new framework we have Windows.Networking.Sockets. The exact question is: How to send data from the Client to the Server?

Here is what happens when the user clicks on Send button:

var writer = new DataWriter(socket.OutputStream);
writer.WriteString(message);
var ret = await writer.StoreAsync();
writer.DetachStream();
LogMessage(string.Format("Sent (to {0}) {1}", socket.Information.RemoteHostName.DisplayName, message));

At server side:

srReceiver = new System.IO.StreamReader(tcpClient.GetStream());
strResponse = srReceiver.ReadLine();
Mathias Lykkegaard Lorenzen
  • 15,031
  • 23
  • 100
  • 187
Dimitar Gyurov
  • 92
  • 2
  • 15
  • 1
    Wait, what? How is `System.Net.Sockets` not supported in 4.5? – Jon Apr 27 '12 at 10:13
  • 1
    Seems like TCPClient is supported in .net 4.5: http://msdn.microsoft.com/en-us/library/zc1d0e0f(v=vs.110).aspx –  Apr 27 '12 at 10:17
  • 1
    Is the client a WinRT app? (This could explain no `System.Net`.) – Richard Apr 27 '12 at 10:25
  • The client is a metro application, so in visual studio 2011 beta, the only available namespaces from System.Net, which I can use are: System.Net.Http System.NetworkInformation There is not System.Net.Sockets :( – Dimitar Gyurov Apr 27 '12 at 11:03
  • http://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.sockets.aspx – Hans Passant Apr 27 '12 at 11:35
  • http://stackoverflow.com/questions/10350465/how-to-send-a-simple-message-from-winrt-client-net-4-5-to-desktop-server-net This is better written question. Please read it if you are interested in this topic and want to receive better understanding of the problem. – Dimitar Gyurov Apr 27 '12 at 12:43
  • Don't create a new question: edit this one. – Richard Apr 27 '12 at 13:50

1 Answers1

1

Sockets just allow a bidirectional stream of octets. This is independent of the API: Win32 APIs can interoperate with POSIX sockets quite happily (consider browsing content on Windows served from Apache running on Linux).

Anything more is up to the client and server to agree (message encoding etc.).

Ie. make the connection (with different APIs), then send data in the common format and it will be received.

Richard
  • 106,783
  • 21
  • 203
  • 265
  • I know that, but when I try the code above, which is logical for me it's not working. I was not able to send data from the client to the server. I tried with No-Metro Client, everything worked fine. – Dimitar Gyurov Apr 27 '12 at 11:07
  • What is the content of `message` on the client: `ReadLine` will wait for a newline. I would also use SysInternal's TCP View to check that data is sent. Otherwise: what debugging have you done (please update the question). – Richard Apr 27 '12 at 13:53
  • For the message variable I just pass a string which I get from my textfield. I already made the two gui and I connected properly the control with the code, so my expectations are to see the client's message in the server textblock. I also see the packages sent from the client to the server from Wireshark. – Dimitar Gyurov Apr 27 '12 at 14:48
  • 1
    `ReadLine` reads until the next newline character; are you including one in the string you pass to `WriteString`? – Pavel Minaev Apr 27 '12 at 16:20
  • OK. I tried with +"\r\n" and it worked fine. Thank you for the help! – Dimitar Gyurov Apr 27 '12 at 16:40