Results 1 to 2 of 2
  1. #1
    oszc is offline Member
    Join Date
    Aug 2011
    Posts
    20
    Rep Power
    0

    Default Help with Tcp problem

    client:
    Java Code:
    import java.net.*;
    import java.io.*;
    
    public class SendFile {
    
    	public void send()throws Exception{
    		Socket socket = 
    				new Socket (InetAddress.getByName("192.168.1.5"),10000);
    		BufferedInputStream bis = 
    				new BufferedInputStream(new FileInputStream("D:\\hello.txt"));
    		BufferedOutputStream socketOut = new BufferedOutputStream(socket.getOutputStream());
    		
    		byte[] buff = new byte[1024];
    		int len ;
    		while ( (len = bis.read(buff))!=-1)
    		{
    			socketOut.write(buff);
    		}
    		System.out.println("finished");
    		socket.shutdownOutput();
    		BufferedReader socketInput =
    				new BufferedReader( new InputStreamReader(socket.getInputStream()));
    		String s = socketInput.readLine();
    		System.out.println(s);
    		bis.close();
    		socket.close();	
    	}
    	
    	
    	public static void main(String[] args) throws Exception {
    		SendFile s = new SendFile();
    		s.send();
    	}
    
    }
    Server:
    Java Code:
    import java.io.*;
    import java.net.*;
    
    public class ReceiveFile {
     
    	public void Receive() throws Exception{
    		ServerSocket serSocket = new ServerSocket(10000);
    		Socket socket = serSocket.accept();
    		
    		BufferedOutputStream fileOut = 
    				new BufferedOutputStream(new FileOutputStream("E:\\hello.txt"));
    		
    		BufferedInputStream inSocket = 
    				new BufferedInputStream(socket.getInputStream());
    		
    		byte[] buf = new byte[1024];
    		int len =0;
    		System.out.println("Going to While");
    		while ( (len = inSocket.read(buf))!=-1)   // i cant go into this loop 
    		{
    			System.out.println("s"+new String(buf, 0,  len));  //because this line  never printed
    			fileOut.write(buf, 0, len);
    			fileOut.flush();
    			
    		}
    		String s = "finished!";
    		System.out.println(s);
    		DataOutputStream socketOut = new DataOutputStream(socket.getOutputStream());
    		socketOut.writeBytes(s);
    		
    		fileOut.close();
    		socket.close();
    		serSocket.close();		
    	}
    	
    	public static void main(String[] args) throws Exception {
    		ReceiveFile rec = new ReceiveFile();
    		rec.Receive();
    	}
    
    }
    the problem was described inside the code(line 18 in the last code), can anyone help me with that?
    Last edited by oszc; 11-08-2011 at 07:14 AM.

  2. #2
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default Re: Help with Tcp problem

    The problem isn't in the ReceiveFile class, its in the SendFile class. You aren't reading anything out of the file so nothing is being sent to the server. Look at changing how you are trying to read characters out of the "hello.txt" file.

    Edit: Sorry, misspoke. I actually tried running your example and it is reading the file, for some reason just not sending it over the stream.

    Edit 2: So not actually an answer to your problem, but i've never used a BufferedOutputStream for TCP client/server. Changing
    Java Code:
    BufferedOutputStream socketOut = new BufferedOutputStream(socket.getOutputStream());
    
    to
    
    OutputStream socketOut = socket.getOutputStream();
    immediately solves your problem.
    Last edited by Shoss; 11-09-2011 at 06:07 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
  •