473,504 Members | 13,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CSharp Client/Java Server TCP problems

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() );
}

}

}
}

Nov 17 '05 #1
0 2757

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

Similar topics

1
5399
by: Krist | last post by:
Hi All, Our customer has just decided to choose as development standard. The application is HR application with report. While many like Web client, Java application/thick client is still...
1
1479
by: Rafael Grybs | last post by:
Hi Ng, does Csharp provide something correspondig to applet-technlogy in java world? Need some information on this topic. Regards Rafael
2
1747
by: http://vmdd.tech.mylinuxisp.com/catalog/ | last post by:
<quote> Java vs. Csharp I attempted to write a csharp version of sockSvr (java). After months of research, I aborted the project. Csharp (and ..NET) does not have the equivalents of JDBC,...
2
12995
by: Kumar | last post by:
Hi, I am trying to develop FTP client application using c#. Our application functinality should be like this: 1.our client uploads files,folders to our ftp server for every 20 minutes. ...
8
1911
by: DW | last post by:
Hi, Here is my question. I want to push security prices to the desktop from the server. Whenever there is a new price in the database, the server notifies the client. How can this be done in...
0
4337
by: Divyakumar Jain,SAP Netweaver consultant | last post by:
Hi all, I have created an web service on SAP system. I have created a .net client using Micrsoft Visual Studio 2003 and .net frame work 2.0. in Visual Basic. In that .net client i have created...
1
2370
by: fortepianissimo | last post by:
I have a simple xmlrpc server/client written in Python, and the client throws a list of lists to the server and gets back a list of lists. This runs without a problem. I then wrote a simple Java...
4
1469
by: Jon Slaughter | last post by:
How do I go about accessing some client side control? I'm trying to design a web site in asp.net(well, I want to but I'm seeing that JSP might be better. I don't like java but I do like the fact...
0
7213
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7298
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
7366
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...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.