473,703 Members | 4,277 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 RunSocketTcpCli ent()
{
Socket sock = new Socket(AddressF amily.InterNetw ork, SocketType.Stre am,
ProtocolType.Tc p);
IPEndPoint iep = new IPEndPoint(IPAd dress.Parse("12 7.0.0.1"), 8080);
byte[] data = new byte[1024];
string stringData;
int recv=0;
sock.Connect(ie p);
data = Encoding.ASCII. GetBytes("Hello ");
sock.Send(data, data.Length, SocketFlags.Non e);
}

=============== ==
The Java code
=============== ==
//: c15:JabberServe r.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(PO RT);
System.out.prin tln("Started: " + s);
try {
// Blocks until a connection occurs:
Socket socket = s.accept();
try {
System.out.prin tln(
"Connection accepted: "+ socket);
BufferedReader in =
new BufferedReader(
new InputStreamRead er(
socket.getInput Stream()));
// Output is automatically flushed
// by PrintWriter:
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWri ter(
socket.getOutpu tStream())),tru e);
while (true) {
String str = in.readLine();
if (str.equals("EN D")) break;
System.out.prin tln("Echoing: " + str);
out.println(str );
}
// Always close the two sockets...
} finally {
System.out.prin tln("closing... ");
socket.close();
}
} finally {
s.close();
}
}
} ///:~

Thanks.

Nov 17 '05 #1
4 8199
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***@discussi ons.microsoft.c om> wrote in message
news:8E******** *************** ***********@mic rosoft.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 RunSocketTcpCli ent()
{
Socket sock = new Socket(AddressF amily.InterNetw ork, SocketType.Stre am,
ProtocolType.Tc p);
IPEndPoint iep = new IPEndPoint(IPAd dress.Parse("12 7.0.0.1"), 8080);
byte[] data = new byte[1024];
string stringData;
int recv=0;
sock.Connect(ie p);
data = Encoding.ASCII. GetBytes("Hello ");
sock.Send(data, data.Length, SocketFlags.Non e);
}

=============== ==
The Java code
=============== ==
//: c15:JabberServe r.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(PO RT);
System.out.prin tln("Started: " + s);
try {
// Blocks until a connection occurs:
Socket socket = s.accept();
try {
System.out.prin tln(
"Connection accepted: "+ socket);
BufferedReader in =
new BufferedReader(
new InputStreamRead er(
socket.getInput Stream()));
// Output is automatically flushed
// by PrintWriter:
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWri ter(
socket.getOutpu tStream())),tru e);
while (true) {
String str = in.readLine();
if (str.equals("EN D")) break;
System.out.prin tln("Echoing: " + str);
out.println(str );
}
// Always close the two sockets...
} finally {
System.out.prin tln("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(S ocketShutdown.B oth) 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***@discussi ons.microsoft.c om> wrote in message
news:8E******** *************** ***********@mic rosoft.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 RunSocketTcpCli ent()
{
Socket sock = new Socket(AddressF amily.InterNetw ork, SocketType.Stre am,
ProtocolType.Tc p);
IPEndPoint iep = new IPEndPoint(IPAd dress.Parse("12 7.0.0.1"), 8080);
byte[] data = new byte[1024];
string stringData;
int recv=0;
sock.Connect(ie p);
data = Encoding.ASCII. GetBytes("Hello ");
sock.Send(data, data.Length, SocketFlags.Non e);
}

=============== ==
The Java code
=============== ==
//: c15:JabberServe r.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(PO RT);
System.out.prin tln("Started: " + s);
try {
// Blocks until a connection occurs:
Socket socket = s.accept();
try {
System.out.prin tln(
"Connection accepted: "+ socket);
BufferedReader in =
new BufferedReader(
new InputStreamRead er(
socket.getInput Stream()));
// Output is automatically flushed
// by PrintWriter:
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWri ter(
socket.getOutpu tStream())),tru e);
while (true) {
String str = in.readLine();
if (str.equals("EN D")) break;
System.out.prin tln("Echoing: " + str);
out.println(str );
}
// Always close the two sockets...
} finally {
System.out.prin tln("closing... ");
socket.close();
}
} finally {
s.close();
}
}
} ///:~

Thanks.


Nov 17 '05 #3
you can set the option of the socket to Nagle SocketOptionNam e.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 SetSocketOption Method...

--
Vadym Stetsyak aka Vadmyst
"yaron" <ya***@discussi ons.microsoft.c om> wrote in message
news:8D******** *************** ***********@mic rosoft.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(S ocketShutdown.B oth) 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***@discussi ons.microsoft.c om> wrote in message
news:8E******** *************** ***********@mic rosoft.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 RunSocketTcpCli ent()
{
Socket sock = new Socket(AddressF amily.InterNetw ork, SocketType.Stre am, ProtocolType.Tc p);
IPEndPoint iep = new IPEndPoint(IPAd dress.Parse("12 7.0.0.1"), 8080);
byte[] data = new byte[1024];
string stringData;
int recv=0;
sock.Connect(ie p);
data = Encoding.ASCII. GetBytes("Hello ");
sock.Send(data, data.Length, SocketFlags.Non e);
}

=============== ==
The Java code
=============== ==
//: c15:JabberServe r.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(PO RT);
System.out.prin tln("Started: " + s);
try {
// Blocks until a connection occurs:
Socket socket = s.accept();
try {
System.out.prin tln(
"Connection accepted: "+ socket);
BufferedReader in =
new BufferedReader(
new InputStreamRead er(
socket.getInput Stream()));
// Output is automatically flushed
// by PrintWriter:
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWri ter(
socket.getOutpu tStream())),tru e);
while (true) {
String str = in.readLine();
if (str.equals("EN D")) break;
System.out.prin tln("Echoing: " + str);
out.println(str );
}
// Always close the two sockets...
} finally {
System.out.prin tln("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 SocketOptionNam e.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 SetSocketOption Method...

--
Vadym Stetsyak aka Vadmyst
"yaron" <ya***@discussi ons.microsoft.c om> wrote in message
news:8D******** *************** ***********@mic rosoft.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(S ocketShutdown.B oth) 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***@discussi ons.microsoft.c om> wrote in message
news:8E******** *************** ***********@mic rosoft.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 RunSocketTcpCli ent()
> {
> Socket sock = new Socket(AddressF amily.InterNetw ork, SocketType.Stre am, > ProtocolType.Tc p);
> IPEndPoint iep = new IPEndPoint(IPAd dress.Parse("12 7.0.0.1"), 8080);
> byte[] data = new byte[1024];
> string stringData;
> int recv=0;
> sock.Connect(ie p);
> data = Encoding.ASCII. GetBytes("Hello ");
> sock.Send(data, data.Length, SocketFlags.Non e);
> }
>
> =============== ==
> The Java code
> =============== ==
> //: c15:JabberServe r.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(PO RT);
> System.out.prin tln("Started: " + s);
> try {
> // Blocks until a connection occurs:
> Socket socket = s.accept();
> try {
> System.out.prin tln(
> "Connection accepted: "+ socket);
> BufferedReader in =
> new BufferedReader(
> new InputStreamRead er(
> socket.getInput Stream()));
> // Output is automatically flushed
> // by PrintWriter:
> PrintWriter out =
> new PrintWriter(
> new BufferedWriter(
> new OutputStreamWri ter(
> socket.getOutpu tStream())),tru e);
> while (true) {
> String str = in.readLine();
> if (str.equals("EN D")) break;
> System.out.prin tln("Echoing: " + str);
> out.println(str );
> }
> // Always close the two sockets...
> } finally {
> System.out.prin tln("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
9243
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 problems doing this. I wanted to implemented a generic "Helper" class like this: /** * Helper
0
1448
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 data independent of a receive. The send happens just fine, but when I receive the next set of data from the client (in a size, size, data set of packets), I lose however many packets of data I sent. For example, a normal client send would...
11
6637
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 transfer data across.On the serve I am using Socket 2 API (recv function to read bytes)and not using ..NET. I use FileStream to open the file on the pocket pc, then associate a BinaryReader object with the stream and call ReadBytes to read all the...
1
3002
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 IPEndPoint(hostadd, port); Socket soc = new Socke(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp);
5
1325
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 could be open to abuse. For example, if I do some javascript on the submit button, which gets this "seceret" information and munges it onto the Action property of a Form tag, anyone with a basic understanding of javascript would be able to work...
0
3920
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 development client. Bur Its gives me following error message. I searched lots of things on net and tried on remote server but i didnt got suceess. Can any one tell me how to set TCP\IP connection protocol on server for particular instance. Becuase I think
5
24008
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 development client. Bur Its gives me following error message. I searched lots of things on net and tried on remote server but i didnt got suceess. Can any one tell me how to set TCP\IP
0
2866
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 HotSpot(TM) Client VM User home directory = C:\Documents and Settings\noa ---------------------------------------------------- c: clear console window
9
4301
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 some packets in response to the programs first message, but when I receive a response back, it's the last packet I sent. After that packet, I receive the packet I really wanted. This only happens when I connect with local host. I tried...
0
8761
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8674
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9262
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9018
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7876
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6595
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4434
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2463
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.