CSharp Client/Java Server TCP problems

N

netgeni59

Hello fellow C# friends,

I am trying to write a C# TCP client that was formerly written in Java.
The server must still remain in Java.

I cannot get text data from the C# client to be received by the Java
TCP server. No matter how I try to send data from the client to the
server, the Java server DataInputStream readUTF() method never returns
with any data. Can someone please shed some light on this problem?
Thanks.

-Frank

I re-wrote them both in simpler form for demonstration purposes.

Java Based Server:
----------------------

import java.net.*;
import java.io.*;
import java.util.*;

public class ChatServer {
public ChatServer (int port) throws IOException {
ServerSocket server = new ServerSocket (port);
while (true) {
Socket client = server.accept ();
System.out.println ("Accepted from " + client.getInetAddress ());
ChatHandler c = new ChatHandler (client);
c.start ();
}
}

public static void main (String args[]) throws IOException {
new ChatServer (1098);
}
}

import java.net.*;
import java.io.*;
import java.util.*;

public class ChatHandler extends Thread {
protected Socket s;
protected DataInputStream i;
protected DataOutputStream o;

public ChatHandler (Socket s) throws IOException {
this.s = s;
i = new DataInputStream (new BufferedInputStream
(s.getInputStream()));
o = new DataOutputStream (new BufferedOutputStream
(s.getOutputStream()));
}

protected static Vector handlers = new Vector ();

public void run () {
String name = s.getInetAddress ().toString ();
try {
System.out.println(name + " has joined.");
handlers.addElement (this);
while (true) {
System.out.println("Waiting for data...");
String msg = i.readUTF ();
System.out.println(name + " - " + msg);
}
} catch (IOException ex) {
ex.printStackTrace ();
} finally {
handlers.removeElement (this);
System.out.println(name + " has left.");
try {
s.close ();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

C# Client
----------------------

using System;
using System.Threading;
using System.Net.Sockets;
using System.IO;
using System.Data;
using System.Net;
using System.Text;

namespace TCPClient
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Client
{
static NetworkStream output;
static BinaryWriter writer;
static BinaryReader reader;
static Thread readThread;

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
string msg;
TcpClient client;

client = new TcpClient();
client.Connect( "localhost", 1098 );
output = client.GetStream();

// create objects for writing and reading across stream
writer = new BinaryWriter(output, System.Text.Encoding.UTF8);
reader = new BinaryReader(output);

//Start thread to recieve data back from server...
readThread = new Thread(new ThreadStart(RunClient));
readThread.Start();

// get text from colsole to send until just the ENTER key pressed...

do
{

Console.Write("Text to send: ");
msg = Console.ReadLine();
if (msg == "")
break;

Byte[] byteDateLine = Encoding.ASCII.GetBytes( msg.ToCharArray() );
writer.Write(byteDateLine, byteDateLine.Length, 0 );

} while (true);

Console.WriteLine("Closing connection");

readThread.Abort();
writer.Close();
reader.Close();
output.Close();
client.Close();
}


static void RunClient()
{
string msg;

try
{
Console.WriteLine("Waiting for data from server...");
do
{
msg = reader.ReadString();
Console.WriteLine(msg);
} while (true);

}
catch ( Exception error )
{
Console.WriteLine( error.ToString() );
}

}

}
}
 

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