473,410 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

problem when sending data over TCP socke from c# client to java se

Hi,

I have a problem when sending data over TCP socket from c# client to java
server.
the connection established ok, but i can't send data from c# client to java
server.
it's work ok with TcpClient, NetworkStream and StreamWriter classes.

but with low level socket it doesn't work (When using the Socket class Send
method).
here is the sample code (c# and java):
===========
C# Code
===========

public static void RunSocketTcpClient()
{
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8080);
byte[] data = new byte[1024];
string stringData;
int recv=0;
sock.Connect(iep);
data = Encoding.ASCII.GetBytes("Hello");
sock.Send(data, data.Length, SocketFlags.None);
}

=================
The Java code
=================
//: c15:JabberServer.java
// Very simple server that just
// echoes whatever the client sends.
// {RunByHand}
import java.io.*;
import java.net.*;

public class JabberServer {
// Choose a port outside of the range 1-1024:
public static final int PORT = 8080;
public static void main(String[] args)
throws IOException {
ServerSocket s = new ServerSocket(PORT);
System.out.println("Started: " + s);
try {
// Blocks until a connection occurs:
Socket socket = s.accept();
try {
System.out.println(
"Connection accepted: "+ socket);
BufferedReader in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
// Output is automatically flushed
// by PrintWriter:
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),true);
while (true) {
String str = in.readLine();
if (str.equals("END")) break;
System.out.println("Echoing: " + str);
out.println(str);
}
// Always close the two sockets...
} finally {
System.out.println("closing...");
socket.close();
}
} finally {
s.close();
}
}
} ///:~

Thanks.

Nov 17 '05 #1
4 8178
What error Send is returning?
Was the Connect(...) method uccessful - check Connected property of the
socket.

The client is not sending anything at all?

--
Vadym Stetsyak aka Vadmyst
"yaron" <ya***@discussions.microsoft.com> wrote in message
news:8E**********************************@microsof t.com...
Hi,

I have a problem when sending data over TCP socket from c# client to java
server.
the connection established ok, but i can't send data from c# client to java server.
it's work ok with TcpClient, NetworkStream and StreamWriter classes.

but with low level socket it doesn't work (When using the Socket class Send method).
here is the sample code (c# and java):
===========
C# Code
===========

public static void RunSocketTcpClient()
{
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8080);
byte[] data = new byte[1024];
string stringData;
int recv=0;
sock.Connect(iep);
data = Encoding.ASCII.GetBytes("Hello");
sock.Send(data, data.Length, SocketFlags.None);
}

=================
The Java code
=================
//: c15:JabberServer.java
// Very simple server that just
// echoes whatever the client sends.
// {RunByHand}
import java.io.*;
import java.net.*;

public class JabberServer {
// Choose a port outside of the range 1-1024:
public static final int PORT = 8080;
public static void main(String[] args)
throws IOException {
ServerSocket s = new ServerSocket(PORT);
System.out.println("Started: " + s);
try {
// Blocks until a connection occurs:
Socket socket = s.accept();
try {
System.out.println(
"Connection accepted: "+ socket);
BufferedReader in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
// Output is automatically flushed
// by PrintWriter:
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),true);
while (true) {
String str = in.readLine();
if (str.equals("END")) break;
System.out.println("Echoing: " + str);
out.println(str);
}
// Always close the two sockets...
} finally {
System.out.println("closing...");
socket.close();
}
} finally {
s.close();
}
}
} ///:~

Thanks.

Nov 17 '05 #2
Vadym,

there is no error on the send method, just the java server doesn't get thet
data.
i think the problem is that there is no Socket.Flush() method , because if
after the sock.send i call to sock.Shutdown(SocketShutdown.Both) them the
java server gets the sent data.

Thanks.
"Vadym Stetsyak" wrote:
What error Send is returning?
Was the Connect(...) method uccessful - check Connected property of the
socket.

The client is not sending anything at all?

--
Vadym Stetsyak aka Vadmyst
"yaron" <ya***@discussions.microsoft.com> wrote in message
news:8E**********************************@microsof t.com...
Hi,

I have a problem when sending data over TCP socket from c# client to java
server.
the connection established ok, but i can't send data from c# client to

java
server.
it's work ok with TcpClient, NetworkStream and StreamWriter classes.

but with low level socket it doesn't work (When using the Socket class

Send
method).
here is the sample code (c# and java):
===========
C# Code
===========

public static void RunSocketTcpClient()
{
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8080);
byte[] data = new byte[1024];
string stringData;
int recv=0;
sock.Connect(iep);
data = Encoding.ASCII.GetBytes("Hello");
sock.Send(data, data.Length, SocketFlags.None);
}

=================
The Java code
=================
//: c15:JabberServer.java
// Very simple server that just
// echoes whatever the client sends.
// {RunByHand}
import java.io.*;
import java.net.*;

public class JabberServer {
// Choose a port outside of the range 1-1024:
public static final int PORT = 8080;
public static void main(String[] args)
throws IOException {
ServerSocket s = new ServerSocket(PORT);
System.out.println("Started: " + s);
try {
// Blocks until a connection occurs:
Socket socket = s.accept();
try {
System.out.println(
"Connection accepted: "+ socket);
BufferedReader in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
// Output is automatically flushed
// by PrintWriter:
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),true);
while (true) {
String str = in.readLine();
if (str.equals("END")) break;
System.out.println("Echoing: " + str);
out.println(str);
}
// Always close the two sockets...
} finally {
System.out.println("closing...");
socket.close();
}
} finally {
s.close();
}
}
} ///:~

Thanks.


Nov 17 '05 #3
you can set the option of the socket to Nagle SocketOptionName.NoDelay.
It seems to me that Nagle algorithm may be the reason of the send delay.
This algorithm is responsible for preventing network congestion with small
data packets. In your case data sent was not big. You can try to send
greater amount of data and watch for the java server...

To set the option call SetSocketOptionMethod...

--
Vadym Stetsyak aka Vadmyst
"yaron" <ya***@discussions.microsoft.com> wrote in message
news:8D**********************************@microsof t.com...
Vadym,

there is no error on the send method, just the java server doesn't get thet data.
i think the problem is that there is no Socket.Flush() method , because if
after the sock.send i call to sock.Shutdown(SocketShutdown.Both) them the
java server gets the sent data.

Thanks.
"Vadym Stetsyak" wrote:
What error Send is returning?
Was the Connect(...) method uccessful - check Connected property of the
socket.

The client is not sending anything at all?

--
Vadym Stetsyak aka Vadmyst
"yaron" <ya***@discussions.microsoft.com> wrote in message
news:8E**********************************@microsof t.com...
Hi,

I have a problem when sending data over TCP socket from c# client to java server.
the connection established ok, but i can't send data from c# client to

java
server.
it's work ok with TcpClient, NetworkStream and StreamWriter classes.

but with low level socket it doesn't work (When using the Socket class

Send
method).
here is the sample code (c# and java):
===========
C# Code
===========

public static void RunSocketTcpClient()
{
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8080);
byte[] data = new byte[1024];
string stringData;
int recv=0;
sock.Connect(iep);
data = Encoding.ASCII.GetBytes("Hello");
sock.Send(data, data.Length, SocketFlags.None);
}

=================
The Java code
=================
//: c15:JabberServer.java
// Very simple server that just
// echoes whatever the client sends.
// {RunByHand}
import java.io.*;
import java.net.*;

public class JabberServer {
// Choose a port outside of the range 1-1024:
public static final int PORT = 8080;
public static void main(String[] args)
throws IOException {
ServerSocket s = new ServerSocket(PORT);
System.out.println("Started: " + s);
try {
// Blocks until a connection occurs:
Socket socket = s.accept();
try {
System.out.println(
"Connection accepted: "+ socket);
BufferedReader in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
// Output is automatically flushed
// by PrintWriter:
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),true);
while (true) {
String str = in.readLine();
if (str.equals("END")) break;
System.out.println("Echoing: " + str);
out.println(str);
}
// Always close the two sockets...
} finally {
System.out.println("closing...");
socket.close();
}
} finally {
s.close();
}
}
} ///:~

Thanks.


Nov 17 '05 #4
Thanks Vadym,

the problem was that the java server wait for sending Enter (new line
character) by using String str = in.readLine();
so after adding new line char to the end of the string and sending the java
server receive that string.

Thanks again.

"Vadym Stetsyak" wrote:
you can set the option of the socket to Nagle SocketOptionName.NoDelay.
It seems to me that Nagle algorithm may be the reason of the send delay.
This algorithm is responsible for preventing network congestion with small
data packets. In your case data sent was not big. You can try to send
greater amount of data and watch for the java server...

To set the option call SetSocketOptionMethod...

--
Vadym Stetsyak aka Vadmyst
"yaron" <ya***@discussions.microsoft.com> wrote in message
news:8D**********************************@microsof t.com...
Vadym,

there is no error on the send method, just the java server doesn't get

thet
data.
i think the problem is that there is no Socket.Flush() method , because if
after the sock.send i call to sock.Shutdown(SocketShutdown.Both) them the
java server gets the sent data.

Thanks.
"Vadym Stetsyak" wrote:
What error Send is returning?
Was the Connect(...) method uccessful - check Connected property of the
socket.

The client is not sending anything at all?

--
Vadym Stetsyak aka Vadmyst
"yaron" <ya***@discussions.microsoft.com> wrote in message
news:8E**********************************@microsof t.com...
> Hi,
>
> I have a problem when sending data over TCP socket from c# client to java > server.
> the connection established ok, but i can't send data from c# client to
java
> server.
> it's work ok with TcpClient, NetworkStream and StreamWriter classes.
>
> but with low level socket it doesn't work (When using the Socket class
Send
> method).
> here is the sample code (c# and java):
> ===========
> C# Code
> ===========
>
> public static void RunSocketTcpClient()
> {
> Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, > ProtocolType.Tcp);
> IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8080);
> byte[] data = new byte[1024];
> string stringData;
> int recv=0;
> sock.Connect(iep);
> data = Encoding.ASCII.GetBytes("Hello");
> sock.Send(data, data.Length, SocketFlags.None);
> }
>
> =================
> The Java code
> =================
> //: c15:JabberServer.java
> // Very simple server that just
> // echoes whatever the client sends.
> // {RunByHand}
> import java.io.*;
> import java.net.*;
>
> public class JabberServer {
> // Choose a port outside of the range 1-1024:
> public static final int PORT = 8080;
> public static void main(String[] args)
> throws IOException {
> ServerSocket s = new ServerSocket(PORT);
> System.out.println("Started: " + s);
> try {
> // Blocks until a connection occurs:
> Socket socket = s.accept();
> try {
> System.out.println(
> "Connection accepted: "+ socket);
> BufferedReader in =
> new BufferedReader(
> new InputStreamReader(
> socket.getInputStream()));
> // Output is automatically flushed
> // by PrintWriter:
> PrintWriter out =
> new PrintWriter(
> new BufferedWriter(
> new OutputStreamWriter(
> socket.getOutputStream())),true);
> while (true) {
> String str = in.readLine();
> if (str.equals("END")) break;
> System.out.println("Echoing: " + str);
> out.println(str);
> }
> // Always close the two sockets...
> } finally {
> System.out.println("closing...");
> socket.close();
> }
> } finally {
> s.close();
> }
> }
> } ///:~
>
> Thanks.
>


Nov 17 '05 #5

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

Similar topics

2
by: Kent Lewandowski | last post by:
hi all, Recently I wrote some stored procedures using java jdbc code (admittedly my first stab) and then tried to implement the same within java packages (for code reuse). I encountered...
0
by: Lynne | last post by:
I am using the C# asynchronous socket functionality for a server, and it appears to work fine if I just receive data and echo it back to the client. The problem occurs when I try to handle sending...
11
by: Abhishek | last post by:
I have a problem transfering files using sockets from pocket pc(.net compact c#) to desktop(not using .net just mfc and sockets 2 API). The socket communication is not a issue and I am able to...
1
by: Paul Fi | last post by:
I have this client code: string server = "localhost"; int port = 8085; IPHostEntry hostent = Dns.Resolve(server); IPAddress hostadd = hostent.AddressList; IPEndPoint EPhost = new...
5
by: Nick | last post by:
I have the need to return some data back to the server when a Submit button is pressed on a web page. However I dont want anybody to know about the mechanism for sending the data back, becase it...
0
by: Suresh | last post by:
Hi Guys I have Db2 server installed on remote server. i am connecting to that remote server by using VPN. I want to connect that remote DB2 server instance using my local machine DB2...
5
by: Suresh | last post by:
Hi Guys I have Db2 server installed on remote server. i am connecting to that remote server by using VPN. I want to connect that remote DB2 server instance using my local machine DB2...
0
by: NoaGross | last post by:
Hi, I'm relly new in java and I have a problem. I'm using java applet. When using http all ok, but when trying to use https i get: Java Plug-in 1.5.0_10 Using JRE version 1.5.0_10 Java...
9
by: darthghandi | last post by:
I am trying to create a server application using asynchronous sockets. I run into a problem when I try to connect to my server using a non-.net program. I can establish the connection, and send...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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
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
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,...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.