473,586 Members | 2,681 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 8194
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
9216
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
1445
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...
11
6612
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...
1
2996
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
1320
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...
0
3912
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...
5
23993
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...
0
2855
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
4288
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...
0
7912
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...
0
8202
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. ...
0
8338
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7959
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...
0
8216
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3837
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...
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.