Replies
Welcome, guest
Join CrazyEngineers to reply, ask questions, and participate in conversations.
CrazyEngineers powered by Jatra Community Platform
-
@maro-Ce3knx • Jan 14, 2010
- How you set client-server connection?
- What language?
- Using third-party Sockets libraries or the default libraries of the language?
- Provide information as much as you can. -
@manish-r2Hoep • Jan 14, 2010
I am using Java for this and I am using default libraries of the language
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
connection refused connect
but when i try to execute this program in some one else computer it executes successfully -
@sookie-T06sFW • Jan 14, 2010
99.9% I am sure, if your code is working on 1 machine and not in your own machine then definitely the problem is with IP and port no. Are the port nos. you are using in your program free in your machine. Please confirm this.
Thanks and All the Best ! 😀 -
@sookie-T06sFW • Jan 14, 2010
@goyal420 I tried your program in mymachine, in the starting I got error as following in "Client.java" program
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 withss=new ServerSocket(32);
, it worked like below
At server window
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 -
@manish-r2Hoep • Jan 14, 2010
Thank you very much sookie for this..
Now my program is running successfully -
@manish-r2Hoep • Jun 6, 2010
GUys now i want to extend this program between two remote computers
i mean i want to establish connection between two via internet
Can you please explain me what changes i should made to my program for this?
I have also searched on google about this a lot but no satisfactory results -
@manish-r2Hoep • Jun 21, 2010
Come 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 -
@maro-Ce3knx • Jun 21, 2010
Change localhost to the remote machine IP. -
@manish-r2Hoep • Jun 21, 2010
I have already changed it but still it is not working
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 -
@maro-Ce3knx • Jun 22, 2010
Check firewall settings. -
@sookie-T06sFW • 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 -
@manish-r2Hoep • Jun 22, 2010
no 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? -
@sookie-T06sFW • 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?
1. Both the machines are in network.
2. Server Program is running before making Client program to run.
3. The ip used in below statement of Client.java is of Server machine's ip.
con=
new Socket([IP of machine where Server.java is running],8051);//Developed a socket connection by clientOtherwise your entire program is working fine. Problem may be because of network. Just check once if you are able to access your friend's machine through your machine or vice-versa.
Hope it helps ! Revert back if still problem persists. 😀 -
@manish-r2Hoep • Jun 22, 2010
-
@sookie-T06sFW • 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 .