Results 1 to 2 of 2
Thread: Help with Tcp problem
- 11-08-2011, 06:03 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 20
- Rep Power
- 0
Help with Tcp problem
client:
Server: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(); } }
the problem was described inside the code(line 18 in the last code), can anyone help me with that?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(); } }Last edited by oszc; 11-08-2011 at 06:14 AM.
- 11-09-2011, 04:39 PM #2
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
immediately solves your problem.Java Code:BufferedOutputStream socketOut = new BufferedOutputStream(socket.getOutputStream()); to OutputStream socketOut = socket.getOutputStream();
Last edited by Shoss; 11-09-2011 at 05:07 PM.


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks