Here's a little script I just implemented with ref to internet ..i run my server and then my client..
On my client side i input some string into my client side I am su[pposed to get a hello back from the server...This part is not working ..
These are my server and client side codes.Just go through them and tell me what's wrong...
With regards
On my client side i input some string into my client side I am su[pposed to get a hello back from the server...This part is not working ..
Code:
import java.net.*; import java.io.*; public class lab1_Qu2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub try{ System.out.println("chat started........"); chat cht=new chat(); cht.create_socket("localhost",135); System.out.println("chat Ended........"); } catch(IOException e){ System.err.println(e.getMessage()); } } } class chat{ //method to create socket public void create_socket(String host,int port_num) throws IOException { PrintWriter pW=null; Socket hello_sck=new Socket(host,port_num); send_hello("Hello",hello_sck,pW); hello_sck.close(); } //Method to send Hello text public void send_hello(String str,Socket echoSocket,PrintWriter pW){ try { pW = new PrintWriter(echoSocket.getOutputStream(), true); create_stream(echoSocket); pW.print(str); } catch(IOException e) { System.err.println(e.getMessage()); } } //method to create stream public void create_stream(Socket echoSocket) throws IOException{ BufferedReader in = new BufferedReader( new InputStreamReader( echoSocket.getInputStream())); PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true); out.flush(); System.out.println("Socket input started..1"); while (true) { System.out.println("Socket input started..2"); String str = in.readLine(); if (str == "exit") { System.out.println("Socket input started..3"); break; } else { System.out.println("Socket input started..4"); System.out.println(str); } } System.out.println("Socket input started..5"); } } import java.net.*; import java.io.*; public class lab1_Qu2_server { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub lab1_Qu2_server server_app1=new lab1_Qu2_server(); server_app1.chat_server(); } public ServerSocket create_server(int port)throws IOException { return new ServerSocket(port); } public Socket create_client(ServerSocket s) throws IOException{ return s.accept(); } public void create_stream(Socket clientSocket) throws IOException{ PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader( clientSocket.getInputStream())); send_hello("Hello back",out); out.flush(); } private void send_hello(String str,PrintWriter p){ p.println(str); } public void chat_server(){ try { ServerSocket server=create_server(3003); Socket client=create_client(server); create_stream(client); } catch(IOException e) { System.err.println(e.getMessage()); } } }
With regards
Comment