Java examples for Network:Socket
Read from Socket
import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; public class SocketClient{ public static void main(String[] args) throws UnknownHostException, IOException, ClassNotFoundException, InterruptedException{ //get the localhost IP address, if server is running on some other IP, you need to use that InetAddress host = InetAddress.getLocalHost(); Socket socket = null;/*from ww w. ja v a 2 s . c o m*/ ObjectOutputStream oos = null; ObjectInputStream ois = null; for(int i=0; i<5;i++){ //establish socket connection to server socket = new Socket(host.getHostName(), 495); //write to socket using ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); System.out.println("Sending request to Socket Server"); if(i==4)oos.writeObject("exit"); else oos.writeObject("Ciat ciat"); //read the server response message ois = new ObjectInputStream(socket.getInputStream()); String message = (String) ois.readObject(); System.out.println("Message: " + message); //close resources ois.close(); oos.close(); Thread.sleep(100); } } }