Page 1 of 3 123 LastLast
Results 1 to 20 of 60
Like Tree1Likes

Thread: A simple connection between 2 computers

  1. #1
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default 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:
    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!");
    		}
    	}
    }
    Client:
    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();
    	}
    }
    It outputs the "enter pass" but after I enter the password nothing happens and when I shutdown the run I get:
    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)

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,447
    Rep Power
    25

    Default Re: A simple connection between 2 computers

    Try calling flush

  3. #3
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: A simple connection between 2 computers

    where?and what for?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,447
    Rep Power
    25

    Default Re: A simple connection between 2 computers

    When I added flush I got this debugging output when I executed your code:
    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)
    I put the classes in one file and added a main that called the main of both classes for testing.
    C - for printlns in client and S - for server

  5. #5
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: A simple connection between 2 computers

    I added flush
    I think I get you wrong, flush you mean the method right?

    I put the classes in one file
    One package you mean right?

    added a main that called the main of both classes for testing.
    You've added another class that calls to the Client & Server main mehtods?

    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.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,447
    Rep Power
    25

    Default 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.

  7. #7
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default 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?

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,447
    Rep Power
    25

    Default 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.

  9. #9
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default 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!

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,447
    Rep Power
    25

    Default 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
    tnrh1 likes this.

  11. #11
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default 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!

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,447
    Rep Power
    25

    Default Re: A simple connection between 2 computers

    how to send a file from the server and save it on the client computer.
    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.
    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.

  13. #13
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: A simple connection between 2 computers

    ok,
    here is what I tried:
    client:
    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");
    Server:
    Java Code:
    ObjectInputStream inO = new ObjectInputStream(socket.getInputStream());
    		out.println("PeRsIADO161346");
    		out.flush();
    		
    		f1 = (FileReader)inO.readObject();
    So the client sends a password, the server checks if it's correct and if it does it send the file.
    I tryed to cast the Object to FileReader.
    Anyway I get
    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)
    At this part of the prorgram I don't care the case that the password is incorrect.
    Last edited by tnrh1; 01-27-2012 at 04:53 PM.

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,447
    Rep Power
    25

    Default Re: A simple connection between 2 computers

    I tryed to cast the Object to FileReader.
    I thought you wanted to pass the contents of a disk file.
    The FileReader object has some information that would be used to read the file, not the file's contents.

    java.net.SocketException: Connection reset
    Does the Server do anything that could cause this? Like close the connection.

    if(inO.readObject() != null)
    What happens to the object read here?

  15. #15
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: A simple connection between 2 computers

    The FileReader object has some information that would be used to read the file, not the file's contents.
    So which class should I use to hold the exe data in it?and send it to the client.

    Does the Server do anything that could cause this? Like close the connection.
    hmm I couldn't find something wrong ..

    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();
    		
    	}
    What happens to the object read here?
    Sorry, my bad.. I thought I've updated the code in 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();

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,447
    Rep Power
    25

    Default Re: A simple connection between 2 computers

    which class should I use to hold the exe data
    You could read the file's bytes into a byte array and write that.
    I don't know any class that "holds" a file's bytes.

  17. #17
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default 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.

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,447
    Rep Power
    25

    Default Re: A simple connection between 2 computers

    Have you got it working now?

  19. #19
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default 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:
    Java Code:
    byte[]ar = getBytesFromFile(new File("tictactoe.jar"));
    out.write(ar);
    out.close();
    The client recieve that array and at this moment only print the reference, lol.
    Client:
    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();
    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.
    Anyway I get this error:
    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)
    Line 24: out.write(ar);
    Last edited by tnrh1; 01-27-2012 at 11:32 PM.

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,447
    Rep Power
    25

    Default Re: A simple connection between 2 computers

    You should use the value returned by the read method to control the reading of bytes.

Page 1 of 3 123 LastLast

Similar Threads

  1. Playing sounds on other computers
    By jeffpaulwilson in forum AWT / Swing
    Replies: 0
    Last Post: 09-14-2011, 09:25 PM
  2. Help needed!!!!! choosing network computers
    By apoorv in forum Networking
    Replies: 6
    Last Post: 03-22-2011, 05:33 PM
  3. Need to have multiple computers run same java program
    By thehighlander12 in forum Networking
    Replies: 11
    Last Post: 07-07-2010, 02:30 PM
  4. Career Paths in Computers
    By Lil_Aziz1 in forum New To Java
    Replies: 26
    Last Post: 06-30-2010, 01:59 PM
  5. Sharing a game instance over two computers
    By keiys in forum Java Gaming
    Replies: 0
    Last Post: 04-21-2010, 02:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •