Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java client socket to talk to C server socket

 
Abebe Belew
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic