Socket programming questions
whenever i tries to make client and server as my own PC..it refuses connection...I think it is because of fire wall....can anyone help me how to allow some programs that can access my pc?😕😕
Member • Jan 14, 2010
Member • Jan 14, 2010
import java.io.*; import java.net.*; class Client { public static void main(String args[])throws IOException { Socket con=null; PrintWriter out=null; BufferedReader in=null; try { con=new Socket("localhost",32);//Developed a socket connection by client out=new PrintWriter(con.getOutputStream(),true);//Write user input to socket in=new BufferedReader(new InputStreamReader(con.getInputStream()));//Retrieve input from socket } catch(UnknownHostException e) { System.err.println("Sorry host can't be found"); System.exit(1); } catch(IOException ef) { System.err.println("Sorry Input/output fails"); System.exit(1); } try { BufferedReader std=new BufferedReader(new InputStreamReader(System.in)); String inp; while((inp=std.readLine())!=null) { out.println("User input:"+inp);//It will send user input to PrintWriter System.out.println(in.readLine()); } in.close(); std.close(); out.close(); con.close(); } catch(Exception e) { System.out.println(e.getMessage()); } } }For server side
import java.net.*; import java.io.*; class Server { public static void main(String args[])throws IOException { ServerSocket ss=null; try { ss=new ServerSocket(4445); } catch(Exception e) { System.out.print("Sorry can't listen on port 4445"); System.exit(1); } Socket cs=null; PrintWriter out=null; BufferedReader in=null; try { cs=ss.accept(); out=new PrintWriter(cs.getOutputStream(),true); in=new BufferedReader(new InputStreamReader(cs.getInputStream())); } catch(Exception e) { System.out.println(e.getMessage()); System.exit(1); } try { BufferedReader std=new BufferedReader(new InputStreamReader(System.in)); String input,serve; while((input=in.readLine())!="Bye") { System.out.println("Client says:"+input); serve=std.readLine(); out.println(serve); } } catch(Exception e) { System.out.println("second:"+e.getMessage()); } } }The Program is successfull compiled but when i try execute client it simply refuses connection and I got the exception
Member • Jan 14, 2010
Member • Jan 14, 2010
init:But now I replaced
deps-jar:
compile-single:
run-single:
Sorry Input/output fails
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
ss=new ServerSocket(4445);line in your Server.java with
ss=new ServerSocket(32);, it worked like below
init:At client window
deps-jar:
Compiling 1 source file to D:\Java Application\PRACTICE_JAVA\build\classes
compile-single:
run-single:
hi
Client says:User input:hi
hello
Client says:User input:yo !
init:Hope you got the point, if not please read once thoroughly about Socket Programming. I am sure, few articles are here on this forum itself. 😀
deps-jar:
compile-single:
run-single:
hi
hi
yo !
hello
Member • Jan 14, 2010
Member • Jun 6, 2010
Member • Jun 21, 2010
Member • Jun 21, 2010
Member • Jun 21, 2010
import java.net.*; import java.io.*; class Server { public static void main(String args[])throws IOException { ServerSocket ss=null; InetAddress is=InetAddress.getLocalHost(); try { ss=new ServerSocket(8051,1,is); } catch(Exception e) { System.out.print(e.getMessage()); System.exit(1); } Socket cs=null; PrintWriter out=null; BufferedReader in=null; try { cs=ss.accept(); out=new PrintWriter(cs.getOutputStream(),true); in=new BufferedReader(new InputStreamReader(cs.getInputStream())); } catch(Exception e) { System.out.println(e.getMessage()); System.exit(1); } try { BufferedReader std=new BufferedReader(new InputStreamReader(System.in)); String input,serve; while((input=in.readLine())!="Bye") { System.out.println("Client says:"+input); serve=std.readLine(); out.println(serve); } } catch(Exception e) { System.out.println("second:"+e.getMessage()); } } }
import java.io.*; import java.net.*; class Client { public static void main(String args[])throws IOException { Socket con=null; PrintWriter out=null; BufferedReader in=null; //InetAddress is=null; try { //is=InetAddress.getLocalHost(); con=new Socket("117.205.32.1",8051);//Developed a socket connection by client out=new PrintWriter(con.getOutputStream(),true);//Write user input to socket in=new BufferedReader(new InputStreamReader(con.getInputStream()));//Retrieve input from socket } catch(UnknownHostException e) { System.err.println("Sorry host can't be found"); System.exit(1); } catch(IOException ef) { System.err.println("Sorry Input/output fails"); System.exit(1); } try { BufferedReader std=new BufferedReader(new InputStreamReader(System.in)); String inp; while((inp=std.readLine())!=null) { out.println("User input:"+inp);//It will send user input to PrintWriter System.out.println(in.readLine()); } in.close(); std.close(); out.close(); con.close(); } catch(Exception e) { System.out.println(e.getMessage()); } } }These are changes that i have made
Member • Jun 22, 2010
Member • Jun 22, 2010
What problem or exceptions you are getting with your current code?goyal420Come on Guys Please help me in this
I am badly stuck to this
How to establish communication between two computers via internet through socket programming?
Please help me
Member • Jun 22, 2010
Member • Jun 22, 2010
Hi, just now I tried running your code in my machine and my colleague's machine. It worked perfectly fine. Just please make suregoyal420no exception ,no errors at all
when i try to communicate with my friends through this ..
It doesn't work and shows me message "Sorry input/output fails"
I don't understand why?
May be due to wrong port no
But how can i find the port no suitable for this?
con=
new Socket([IP of machine where Server.java is running],8051);//Developed a socket connection by client
Member • Jun 22, 2010
Member • Jun 22, 2010
LAN & internet should not matter. The only thing is both the machines should be in network. Tonight I will try it in internet also. Then will let you know.goyal420HEY have you Tried it through LAN or internet?
I had tried to make connection through internet .