Results 1 to 20 of 60
- 01-26-2012, 09:58 PM #1
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
A simple connection between 2 computers
Hello,
I've tried to create a connection between a client & a server and looks like something went wrong.
The server should say hello, please enter password.
The client reponse with a string, and the server checks if it equals to a certain string and reponse.
Server:
Client:Java Code:import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main (String[]args) throws IOException { ServerSocket serverSocket = null; Socket clientSocket = null; BufferedReader in = null; PrintWriter out = null; String serverLine,clientLine; try{ serverSocket = new ServerSocket(2000); } catch (Exception e) { System.out.println(e); } try{ clientSocket = serverSocket.accept(); } catch (Exception e) { System.out.println("Accept faild"); } in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); out = new PrintWriter(clientSocket.getOutputStream(),true); out.println("Connected! please enter password"); while((clientLine = in.readLine()) != null) { System.out.println(clientLine); if(clientLine.equalsIgnoreCase("bye")) { out.println("bye bye!"); break; } if(clientLine.equals("persiado")) out.println("Password is correct!"); else out.println("Password is wrong!"); } } }
It outputs the "enter pass" but after I enter the password nothing happens and when I shutdown the run I get:Java Code:import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; public class Client extends Thread { public static void main(String[]args) throws IOException { Socket socket = null; PrintWriter out = null; BufferedReader in = null; try { socket = new Socket("127.0.0.1",2000); out = new PrintWriter(socket.getOutputStream()); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); } catch (Exception e) { System.out.println(e); } BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); String serverLine,clientLine=""; while((serverLine = in.readLine()) != null) { System.out.println("Server: "+serverLine); clientLine = stdIn.readLine(); if(clientLine != null) { out.println(clientLine); if(clientLine.equalsIgnoreCase("bye")) break; } } out.close(); in.close(); stdIn.close(); socket.close(); } }
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at Server.main(Server.java:36)
- 01-27-2012, 01:54 AM #2
Re: A simple connection between 2 computers
Try calling flush
- 01-27-2012, 02:07 AM #3
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
Re: A simple connection between 2 computers
where?and what for?
- 01-27-2012, 02:37 AM #4
Re: A simple connection between 2 computers
When I added flush I got this debugging output when I executed your code:
I put the classes in one file and added a main that called the main of both classes for testing.Running: F:\Java\jre6\bin\java.exe -classpath D:\JavaDevelopment;.;..\. -Xmx512M ClientServer_3
C - Server: S - Connected! please enter password
S - persiado
C - Server: Password is correct!
S - bye
0 error(s)
C - for printlns in client and S - for server
- 01-27-2012, 02:44 AM #5
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
Re: A simple connection between 2 computers
I think I get you wrong, flush you mean the method right?I added flush
One package you mean right?I put the classes in one file
You've added another class that calls to the Client & Server main mehtods?added a main that called the main of both classes for testing.
Your output is correct and seems your changes fixed my errors but I'm not really sure what you did.
Copying is easy but you learn nothing from that, I prefer to understand what I'm doing.
Thanks.
- 01-27-2012, 02:48 AM #6
Re: A simple connection between 2 computers
Yes, I meant the flush() method.
I put your two classes into one class for testing and added a main method that created two threads and called your classes' main methods. I file, no package.
I also removed the console I/O and got the client's messages from an array.
The idea being: when I execute the code, it runs itself and prints out the debug stuff.
- 01-27-2012, 02:55 AM #7
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
Re: A simple connection between 2 computers
I get you, so when the inputs are done automatically by the code the order is not changed.
But incase I do want to input from the console?
All I want is to get a message from the server saying hello enter password, client entering one and the server repons true or false.
Sorry for beeing annoying but where to call the flush method?after which line?
- 01-27-2012, 02:59 AM #8
Re: A simple connection between 2 computers
My merging of the code was for first level testing. To work out one set of bugs. Later you'd separate the code as you've done and run from two PCs.
Put the call to flush() after you write output.
- 01-27-2012, 03:06 AM #9
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
Re: A simple connection between 2 computers
Thank you so much, now everything is working correctly!
But what is that flush method? the api doesn't help much "Flushes the stream. "
Why it didn't worked without it?
Thanks again!
- 01-27-2012, 03:09 AM #10
Re: A simple connection between 2 computers
Some classes will save data in a buffer until its full before writing it out. I think flush() pushes it out of the buffer.
Some where there must be more details on buffering and what flush does. I don't have a link for that.
Maybe somewhere here:
The Really Big Index
- 01-27-2012, 04:13 PM #11
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
Re: A simple connection between 2 computers
Hey again,
I changed a little bit the code.
There is no any while condition, the client sends(literly) a value to the server, the server varify that it's the correct code and (here I need help) sends a file to the client.
The client recieve the file and save it somewhere.
So how do I send a file?I used FileReader to read the file and sent it with out.println(Object x) but I can't find a method that recieve an object ..
all the in. methods returns int types and string etc..
So generally my question is how to send a file from the server and save it on the client computer.
Thanks!
- 01-27-2012, 04:21 PM #12
Re: A simple connection between 2 computers
If you think of a file as a bunch/string of bytes, then to send a file you read the bytes on the server, send them over the net and save them on the client.how to send a file from the server and save it on the client computer.
You'd probably want some protocol with a header record to describe the file that is being send, its name and length.
If you want to send an instance of an object, then both sides need a definition of the class and you use the writeObject of the ObjectOutputStream class. The class would have to implement Serializable.
- 01-27-2012, 04:48 PM #13
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
Re: A simple connection between 2 computers
ok,
here is what I tried:
client:
Server:Java Code:ObjectInputStream inO = new ObjectInputStream(socket.getInputStream()); out.println("PeRsIADO161346"); out.flush(); if(inO.readObject() != null) f1 = (FileReader)inO.readObject(); else System.out.println("Couldn't pass the file");
So the client sends a password, the server checks if it's correct and if it does it send the file.Java Code:ObjectInputStream inO = new ObjectInputStream(socket.getInputStream()); out.println("PeRsIADO161346"); out.flush(); f1 = (FileReader)inO.readObject();
I tryed to cast the Object to FileReader.
Anyway I get
At this part of the prorgram I don't care the case that the password is incorrect.Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.io.ObjectInputStream$PeekInputStream.read(Unk nown Source)
at java.io.ObjectInputStream$PeekInputStream.readFull y(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.rea dShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at Client.main(Client.java:28)Last edited by tnrh1; 01-27-2012 at 04:53 PM.
- 01-27-2012, 07:22 PM #14
Re: A simple connection between 2 computers
I thought you wanted to pass the contents of a disk file.I tryed to cast the Object to FileReader.
The FileReader object has some information that would be used to read the file, not the file's contents.
Does the Server do anything that could cause this? Like close the connection.java.net.SocketException: Connection reset
What happens to the object read here?if(inO.readObject() != null)
- 01-27-2012, 10:41 PM #15
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
Re: A simple connection between 2 computers
So which class should I use to hold the exe data in it?and send it to the client.The FileReader object has some information that would be used to read the file, not the file's contents.
hmm I couldn't find something wrong ..Does the Server do anything that could cause this? Like close the connection.
Java Code:public static void main (String[]args)throws IOException { ServerSocket serverSocket = null; Socket clientSocket = null; BufferedReader in = null; FileReader f1; try{ serverSocket = new ServerSocket(2000); } catch (Exception e) { System.out.println(e); } try{ clientSocket = serverSocket.accept(); } catch (Exception e) { System.out.println(e); } f1 = new FileReader(new File("Notepad++.exe")); in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream()); if(in.readLine().equals("PeRsIADO161346")); out.writeObject(f1); in.close(); out.close(); }Sorry, my bad.. I thought I've updated the code in here.What happens to the object read here?
Client:
Java Code:ObjectInputStream inO = new ObjectInputStream(socket.getInputStream()); out.println("PeRsIADO161346"); out.flush(); f1 = (FileReader)inO.readObject(); out.close(); in.close(); socket.close();
- 01-27-2012, 10:47 PM #16
Re: A simple connection between 2 computers
You could read the file's bytes into a byte array and write that.which class should I use to hold the exe data
I don't know any class that "holds" a file's bytes.
- 01-27-2012, 11:06 PM #17
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
Re: A simple connection between 2 computers
Ok I cleared up my code from some unnecessary declarations and stuff.
I created a file instance for my exe and transfered it into byte array and sent it with "out.write(ar);", as the api says the write method "Writes an array of bytes".
Now this part is alittle bit confusing, there is no any method that from that stream array byte.
So I read it like that: f1 = (File)in.readObject(); and cast it into File as you can see.
And I keep get the same error! which comes from this line f1 = ....
edit:
I am so stupid, I think I got it!
I will read from the stream and save it into byte array with "read(byte[]array)" and then transfer the bytes into a file and save it!! brb testing.Last edited by tnrh1; 01-27-2012 at 11:08 PM.
- 01-27-2012, 11:12 PM #18
Re: A simple connection between 2 computers
Have you got it working now?
- 01-27-2012, 11:28 PM #19
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
Re: A simple connection between 2 computers
I decreased the code complexity, there is no more password verifications right now.
So when the connection been made the server sends the bytes array and close it self.
That's how it looks from the server:
The client recieve that array and at this moment only print the reference, lol.Java Code:byte[]ar = getBytesFromFile(new File("tictactoe.jar")); out.write(ar); out.close();
Client:
I had a dilemma about the array length, cause I can't know what the file length so I used the wrost case.. 9999 lol.Java Code:byte[]ar = new byte[9999]; in.read(ar); //Reads some number of bytes from the input stream and stores them into the buffer array b. System.out.println(ar); socket.close();
Anyway I get this error:
Line 24: out.write(ar);Exception in thread "main" java.net.SocketException: Software caused connection
abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.ObjectOutputStream$BlockDataOutputStream.w rite(Unknown Source)
at java.io.ObjectOutputStream.write(Unknown Source)
at Server.main(Server.java:24)Last edited by tnrh1; 01-27-2012 at 11:32 PM.
- 01-28-2012, 12:01 AM #20
Similar Threads
-
Playing sounds on other computers
By jeffpaulwilson in forum AWT / SwingReplies: 0Last Post: 09-14-2011, 09:25 PM -
Help needed!!!!! choosing network computers
By apoorv in forum NetworkingReplies: 6Last Post: 03-22-2011, 05:33 PM -
Need to have multiple computers run same java program
By thehighlander12 in forum NetworkingReplies: 11Last Post: 07-07-2010, 02:30 PM -
Career Paths in Computers
By Lil_Aziz1 in forum New To JavaReplies: 26Last Post: 06-30-2010, 01:59 PM -
Sharing a game instance over two computers
By keiys in forum Java GamingReplies: 0Last Post: 04-21-2010, 02:39 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks