This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes Java client socket to talk to C server socket Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Java client socket to talk to C server socket" Watch "Java client socket to talk to C server socket" New topic
Author

Java client socket to talk to C server socket

Abebe Belew
Greenhorn

Joined: Jul 21, 2004
Posts: 19
I am trying to write java client-socket to talk to C server-socket.
my application does not read from the server, though it send over the data from the client in the first run. when I try to run in again is just hanhs. Here is my code, please help.

import java.io.*;
import java.net.*;

public class db_connect {

public static void main(String[] args) throws IOException {

Socket socket = null;
PrintStream out = null;
BufferedInputStream in = null;

String msg_out = "1234";
byte[] bytes = msg_out.getBytes();


try {
socket = new Socket( "ip", port);
out = new PrintStream(socket.getOutputStream());
in = new BufferedInputStream(socket.getInputStream());


} catch (UnknownHostException e) {
System.err.println("Don't know about host: hostname");

} catch (IOException e) {
System.err.println("Couldn't get I/O for the on :hostname");
}

if (socket != null) {
try {

out.write(bytes, 0, bytes.length); //sends data to server
out.flush();

int c;
while ((c = in.read()) !=-1){
System.out.write(c);

}
in.close();
out.close();
socket.close();

} catch (UnknownHostException e) {
System.err.println("Trying to connect to unknown host: " + e);
} catch (IOException e) {
System.err.println("IOException: " + e);
}
}
}
}

[ September 15, 2004: Message edited by: Sewsew ]
[ September 15, 2004: Message edited by: Sewsew ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java client socket to talk to C server socket