• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Java client socket to talk to C server socket

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic