Problem java talking to VB.net using sockets

creenmachine

New member
Joined
Jul 20, 2006
Messages
1
Programming Experience
Beginner
Hi!

Hoi,

Currently I'm working on a project that needs to have a java application talking to a VB.net application using sockets... This seems to work, but unfortunatly it works only once :confused:
The idea is that the VB app is a socketserver. The java app connects to VB, sends some data, receives some data back from VB and disconencts... The VB app now waits for a new connection...

Like I mentioned, using the current code it works when I start the java code: The VB server receives the data, sends a string back, the java code finishes and the VB code waits for a new connection.
In this situation, when I run the java code again (with the vb app still waiting) something goes wrong: The vb app seems to stop receiving the data after the first received character... The java side reports this error: "IOException: java.net.SocketException: Connection reset"
If I send only ONE character there is no problem, unfortunately this is not an option...


The VB.net code looks as follows:

**************************

VB.NET:
Imports System.Net.Sockets
Imports System.Text
Imports System.Net
 
Class TCPSrv
Shared Sub Main()
' Must listen on correct port- must be same as port client wants to connect on.
Const portNumber As Integer = 8000
 
Dim teller As Integer
teller = 0
 
Do Until teller = 5
Dim server_ip_address As IPAddress
server_ip_address = IPAddress.Parse("10.1.1.6")
Dim tcpListener As New TcpListener(server_ip_address, portNumber)
tcpListener.Start()
Console.WriteLine("Waiting for connection...")
Try
'Accept the pending client connection and return a TcpClient initialized for communication. 
Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
Console.WriteLine("Connection accepted.")
' Get the stream
Dim networkStream As NetworkStream = tcpClient.GetStream()
' Read the stream into a byte array
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
' Return the data received from the client to the console.
Dim clientdata As String = Encoding.ASCII.GetString(bytes)
Console.WriteLine(("Client sent: " + clientdata))
Dim responseString As String = "Connected to server."
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
networkStream.Write(sendBytes, 0, sendBytes.Length)
Console.WriteLine(("Message Sent /> : " + responseString))
'Any communication with the remote client using the TcpClient can go here.
'Close TcpListener and TcpClient.
tcpClient.Close()
tcpListener.Stop()
Console.WriteLine("klaar")
Console.WriteLine(teller)
teller = teller + 1
Catch e As Exception
Console.WriteLine(e.ToString())
Console.ReadLine()
End Try
Loop
End Sub
End Class
**************************

I have to say that I am more familiar with Java than I am with VB and I found this piece of code on the net... Before I edited the code a bit it was supposed to run only once, so i put a loop in it so it waits for a new connection...

I certainly hope there is someone who can help me with this one...

thanx...



**************************
To be complete, this is the java code:

VB.NET:
public String sendSocketMessage(String address, int port, String data){
// declaration section:
Socket smtpSocket = null; 
DataOutputStream os = null;
DataInputStream is = null;
 
// Initialization section:
try {
smtpSocket = new Socket(address, port);
os = new DataOutputStream(smtpSocket.getOutputStream());
is = new DataInputStream(smtpSocket.getInputStream());
} catch (UnknownHostException e) {
System.err.println("Don't know about host: "+address);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: "+address+" (Is the other socket-side running?)");
}
 
// If everything has been initialized then we want to write some data
// to the socket we have opened
 
if (smtpSocket != null && os != null && is != null) {
try {
 
os.writeBytes(data+"\n");
 
String responseLine;
while ((responseLine = is.readLine()) != null) {
System.out.println("Server: " + responseLine);
if (responseLine.indexOf("Ok") != -1) {
System.out.println("Received Ok...");
break;
}
}
 
// clean up:
os.close();
is.close();
smtpSocket.close(); 
} catch (UnknownHostException e) {
System.err.println("Trying to connect to unknown host: " + e);
} catch (IOException e) {
System.err.println("IOException: " + e);
}
} 
 
return "ok";
}
**************************
 
Last edited by a moderator:
Move the tcpListener.Stop() out of the loop, else it won't be listening and accepting new connections.
 
Back
Top