Note that there are some explanatory texts on larger screens.

plurals
  1. POJava socket application sends additional bytes while trying to send file
    primarykey
    data
    text
    <p>My program is a typicall server-client application. The problem is, when I try to send a file, for some reason client socket recives more bytes than server socket sends. To make it more strange, those bytes are at the beginning, and their number changes depending on file size. When I tried sending 1x1px file, there was 6bytes offset (6 bytes of added data, which, if I skipped them, gave proper image) , whereas 800x600 byte has larger one (totally second output file is bigger than second input by 56 bytes). After searching over the internet the only 2 suggestions I found were to try to clear the bufer beforehand, and I think I tried but it simply "ate" my first inStream.read(). The second was connected to the way pacages are sent, that the size has to be multiply of some number? But it seemed strange, for wouldn't they add some space at the end of the file, not at the beginning?</p> <p>Client class</p> <pre><code>public class Client{ import java.io.BufferedReader; public class Client implements Runnable { static Socket clientSocket = null; static Socket iClientSocket = null; static PrintWriter out = null; static BufferedReader in = null; static InputStream iin = null; public static void main(String[] args) { int port = Integer.valueOf(args[1]); String host = args[0]; try { clientSocket = new Socket(host, port); iClientSocket = new Socket(host, port); out = new PrintWriter(clientSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader( clientSocket.getInputStream())); iin = iClientSocket.getInputStream(); } catch (UnknownHostException e) { System.err.println("Don't know about host: " + host); System.exit(-1); } catch (IOException e) { System.err.println("Couldn't get I/O for " + "the connection to: " + host); System.exit(-1); } BufferedReader stdIn = new BufferedReader(new InputStreamReader( System.in)); String userInput; try { new Thread(new Client()).start(); while ((userInput = stdIn.readLine()) != null) { out.println(userInput); } System.out.println("Closing sockets, closing streams"); out.close(); in.close(); stdIn.close(); iClientSocket.close(); clientSocket.close(); } catch (IOException e) { System.exit(-1); } } @Override public void run() { String a = null; try { while (true) { if((a = in.readLine()) == null) continue; //System.out.println("Tried to read"); int n; try{ n = Integer.valueOf(a); }catch(NumberFormatException e){ System.out.println(a); n=1; //continue; } a = ""; for (int i = 0; i &lt; n; i++) a += in.readLine() + "\n"; System.out.println(a); // if(a.contains("POST"),) if (a.compareToIgnoreCase("EXIT") == 0) { System.out.println("Exiting"); break; } if (a.endsWith("Sending File\n")) { System.out.println("Recieving image."); a=in.readLine(); a=in.readLine(); FileOutputStream iout = new FileOutputStream(a); int fileSize=Integer.parseInt(in.readLine()); int total=0; int step = 1500; int bufferSize = 0; // if(step&gt;fileSize) // bufferSize=(int) fileSize; // else bufferSize = step; byte[] buffer = new byte[bufferSize]; int read=0; while (fileSize-total&gt;0 &amp;&amp; (read = iin.read(buffer, 0, (int)Math.min(buffer.length, fileSize-total))) &gt; 0) { iout.write(buffer, 0, read); //out.println("saved packet"); total+=read; } iout.flush(); iout.close(); System.out.println("Image recieved"); } } } catch (IOException e) { System.exit(-1); } } </code></pre> <p>}</p> <p>Server class, responsible for sending</p> <pre><code>public class Server implements Runnable { static ServerSocket serverSocket; Socket tempSocket; Socket tempSocket2; static volatile List&lt;User&gt; usersList = new ArrayList&lt;User&gt;(); static boolean waitForNew = true; PrintWriter tempOut; volatile User[] tempUser; volatile boolean isReadingN = false; public Server(Socket _s, Socket _s2) { tempSocket = _s; tempSocket2 = _s2; } public Server(PrintWriter nOut, User[] user) { tempOut = nOut; tempUser = user; isReadingN = true; } @Override public void run() { if (isReadingN) { while (true) { if (tempUser != null &amp;&amp; tempUser.length &gt; 0 &amp;&amp; tempUser[0] != null) break; } User[] myUser = new User[1]; myUser[0] = tempUser[0]; // myUser[0]=usersList. while (true) { if (myUser[0].isCurrentlyLoggedIn() == false) break; String[] toSend = null; if (myUser[0].isNotificable()) toSend = myUser[0].printNotifications().split("\n"); else continue; //tempOut.println(""); tempOut.println("1"); tempOut.println(myUser[0].getName()); int sendL=toSend.length; tempOut.println(String.valueOf(sendL)); for (int i = 0; i &lt; toSend.length; i++) tempOut.println(toSend[i]); } return; } Socket clientSocket = tempSocket; System.out.println("Initiating conversation with the client"); String inputLine; try { System.out.print("creating server out..."); PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); Socket iClientSocket = tempSocket2; ObjectOutputStream iout = new ObjectOutputStream( iClientSocket.getOutputStream()); System.out.println("OK!"); System.out.print("creating server in..."); BufferedReader in = new BufferedReader(new InputStreamReader( clientSocket.getInputStream())); System.out.println("OK!"); System.out.print("creating server image streams..."); System.out.println("OK!"); System.out.println("Server initiating conversation"); User[] currentUser = new User[1]; new Thread(new Server(out, currentUser)).start(); while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); boolean[] downloadPicture = new boolean[1]; downloadPicture[0] = false; String input = Command.call(inputLine, currentUser, usersList, downloadPicture); String[] toSend; if (input != null) { toSend = input.split("\n"); } else toSend = new String[0]; out.println(String.valueOf(toSend.length)); for (int i = 0; i &lt; toSend.length; i++) out.println(toSend[i]); if (downloadPicture[0]) { out.println("1"); out.println("Sending File"); System.out.println("Sending File"); String[] temp = inputLine.split(" "); String path = temp[temp.length - 1]; path = path.replace("\\", "\\\\"); File f = new File(path); temp=path.split("\\\\"); String fileName=temp[temp.length-1]; out.println("1"); out.println(fileName); if (f.exists()) { FileInputStream iin = new FileInputStream(path); long fileSize = f.length(); out.println(fileSize); int step = 1500; int bufferSize = 0; if (step &gt; fileSize) bufferSize = (int) fileSize; else bufferSize = step; int bytesRead = 0; byte[] buffer = new byte[bufferSize]; iout.flush(); while ((bytesRead = iin.read(buffer)) &gt; 0) { //iout.flush(); iout.write(buffer, 0, bytesRead); // System.out.println(in.readLine()); } // iout.write(buffer,0,0); iout.flush(); System.out.println("File sent."); iin.close(); } else{ out.println("1"); out.println("Error: File does not exit.");} } else //out.println(" "); if (inputLine.equals("EXIT")) { waitForNew = false; break; } } // End communication graciously System.out.println("Closing sockets, closing streams"); out.close(); in.close(); clientSocket.close(); serverSocket.close(); } catch (IIOException e) { System.out.println("Error: Could not find file"); e.printStackTrace(); System.exit(-1); } catch (IOException e) { System.out.println("Error"); e.printStackTrace(); System.exit(-1); } } public static void main(String[] args) { // Create socket on port given in argument, localhost if (args.length == 0) { System.out .println("Not enough arguments. Try Server &lt;port number&gt;"); System.exit(-1); } int port = 0; try { port = Integer.valueOf(args[0]); System.out.println("Application start"); serverSocket = new ServerSocket(port); System.out.println("Created socket on port " + port); } catch (NumberFormatException c) { System.out .println("Incorrect port number. Try Server &lt;port number&gt;"); System.exit(-1); } catch (IOException e) { System.exit(-1); } // Waiting for client System.out.println("Waiting for client..."); Socket clientSocket = null; Socket iClientSocket = null; while (waitForNew) { try { clientSocket = serverSocket.accept(); iClientSocket = serverSocket.accept(); new Thread(new Server(clientSocket, iClientSocket)).start(); } catch (IOException e) { System.out.println("Accept failed: " + port); System.exit(-1); } } } } </code></pre> <p>Command class, manipulating commands on server side, returning output.</p> <pre><code> public class Command { static volatile List&lt;Status&gt; statusList = new ArrayList&lt;Status&gt;(); static Object block_statusList = new Object(); public static String call(String arg, User[] user, List&lt;User&gt; userlist, boolean[] downloadPicture) { String command[] = arg.split(" "); if (command.length == 0) return ""; else if (command[0].compareTo("LOGIN") == 0) return login(command, user, userlist); else if (command[0].compareTo("EXIT") == 0) return exit(command, user, userlist); else if (user == null || user.length != 1 || user[0] == null || !user[0].isCurrentlyLoggedIn()) return "You need to login first. Try LOGIN &lt;username&gt;"; else if (command[0].compareTo("LOGOUT") == 0) return logout(command, user, userlist); else if (command[0].compareTo("POST") == 0) return post(command, user, userlist, downloadPicture); else if (command[0].compareTo("TIMELINE") == 0) return timeline(command, user, userlist); else if (command[0].compareTo("FOLLOW") == 0) return follow(command, user, userlist); else if (command[0].compareTo("UNFOLLOW") == 0) return unfollow(command, user, userlist); else if (command[0].compareTo("VIEWSTATUS") == 0) return viewstatus(command, user, userlist); else if (command[0].compareTo("LISTUSER") == 0) return listuser(command, user, userlist); else if (command[0].compareTo("VIEWUSER") == 0) return viewuser(command, user, userlist); else if (command[0].compareTo("COMMENT") == 0) return comment(command, user, userlist); else return "Command not recognized"; } private static String logout(String[] arg, User[] user, List&lt;User&gt; userlist) { if (arg.length != 1) return "Wrong number of parameters. LOGOUT does not take any parameters"; user[0].setCurrentlyLoggedIn(false); return "Logged out."; } private static String[] parseArg(String[] arg) { int startCommas = 0; int endCommas = 0; for (int i = 0; i &lt; arg.length; i++) { if (startCommas == 0 &amp;&amp; arg[i].startsWith("\"")) startCommas = i; if (startCommas != 0 &amp;&amp; arg[i].endsWith("\"")) endCommas = i; } if (startCommas != endCommas) { String[] newArg = new String[arg.length + startCommas - endCommas]; for (int i = 0; i &lt; startCommas; i++) newArg[i] = arg[i]; newArg[startCommas] = ""; for (int i = startCommas; i &lt; endCommas; i++) newArg[startCommas] += arg[i] + " "; newArg[startCommas] += arg[endCommas]; for (int i = endCommas + 1; i &lt; arg.length; i++) newArg[i - endCommas] = arg[i]; return newArg; } return arg; } private static String comment(String[] arg, User[] user, List&lt;User&gt; userlist) { synchronized (block_statusList) { arg = parseArg(arg); int SID; if (arg.length != 3) return "Wrong number of parameters. Try COMMENT &lt;Status ID&gt; \"Comment text\""; if (arg[2].charAt(0) != '"' || arg[2].charAt(arg[1].length() - 1) != '"') return "Text of message has to be in quotation marks. Try POST \"post text\""; String temp = arg[2].substring(1, arg[2].length() - 1); try { SID = Integer.valueOf(arg[1]); } catch (NumberFormatException e) { return "Status ID has to be a number. Try COMMENT &lt;Status ID&gt; \"Comment text\""; } Comment c = new Comment(temp, user); statusList.get(SID).addComment(c); return "Comment succeeded"; } } private static String viewuser(String[] arg, User[] user, List&lt;User&gt; userlist) { synchronized (block_statusList) { if (arg.length != 2) return "Wrong number of parameters. Try VIEWUSER &lt;user&gt;"; String r = ""; for (User u : userlist) if (u.getName().compareTo(arg[1]) == 0) r = u.printUser(); return r; } } private static String listuser(String[] arg, User[] user, List&lt;User&gt; userlist) { if (arg.length != 1) return "Wrong number of parameters. LISTUSER doesn't take any parameters"; String r = ""; for (User u : userlist) r += u.getName() + "\n"; return r; } private static String viewstatus(String[] arg, User[] user, List&lt;User&gt; userlist) { synchronized (block_statusList) { int SID; if (arg.length != 2) return "Wrong number of parameters. Try VIEWSTATUS &lt;Status ID&gt;"; try { SID = Integer.valueOf(arg[1]); } catch (NumberFormatException e) { return "Status ID has to be a number. Try VIEWSTATUS &lt;Status ID&gt;"; } if (SID &gt;= statusList.size()) return "Status ID too big."; return statusList.get(SID).printWithComments(); } } private static String unfollow(String[] arg, User[] user, List&lt;User&gt; userlist) { if (arg.length != 2) return "Wrong number of parameters. Try UNFOLLOW &lt;username&gt;"; boolean removed = false; for (User u : userlist) { if (u.getName().compareTo(arg[1]) == 0) { user[0].removeFriend(arg[1]); u.removeFollower(user[0].getName()); removed = true; } } if (!removed) return "User not found."; return "Follow succeded. " + user[0].printFriends(); } private static String follow(String[] arg, User[] user, List&lt;User&gt; userlist) { if (arg.length != 2) return "Wrong number of parameters. Try FOLLOW &lt;username&gt;"; if(user[0]!=null &amp;&amp; user.length!=0 &amp;&amp; user[0]!=null &amp;&amp; user[0].getName().compareTo(arg[1])==0) return "You cannot follow yourself. Try FOLLOW &lt;username&gt;"; User[] followed = null; for (User u : userlist) { if (u.getName().compareTo(arg[1]) == 0) { followed = new User[1]; followed[0] = u; user[0].addFriend(followed); followed[0].addFollower(user); } } if (followed == null) return "User not found."; return "Follow succeded. Current " + user[0].printFriends(); } public static String login(String[] arg, User[] user, List&lt;User&gt; userslist) { if (arg.length != 2) return "Wrong number of parameters. Try LOGIN &lt;username&gt;"; if (user.length &gt; 0 &amp;&amp; user[0] != null) { if (user[0].isCurrentlyLoggedIn()) return "User already logged in. Try logging out first"; } for (User l : userslist) { if (l.getName().compareTo(arg[1]) == 0) { if (l.isCurrentlyLoggedIn()) return "That user is already logged in."; user[0] = l; user[0].setCurrentlyLoggedIn(true); return "Login succeeded"; } } User newUser = new User(arg[1]); userslist.add(newUser); user[0] = newUser; user[0].setCurrentlyLoggedIn(true); return "Login succeeded. Created new user."; } public static String post(String[] arg, User[] user, List&lt;User&gt; userslist, boolean[] downloadPicture) { arg = parseArg(arg); if (arg.length != 2 &amp;&amp; arg.length != 3) return "Wrong number of parameters. Try POST \"post text\""; if (arg[1].charAt(0) != '"' || arg[1].charAt(arg[1].length() - 1) != '"') return "Text of message has to be in quotation marks. Try POST \"post text\""; String temp = arg[1].substring(1, arg[1].length() - 1); Status s; try { s = new Status(temp, statusList.size(), user); } catch (Exception e) { System.out.println("Error: Could not create Status"); e.printStackTrace(); return "Error"; } if(arg.length==3) s.hasImage(arg[2]); user[0].addStatus(s); statusList.add(s); if (arg.length == 3) downloadPicture[0] = true; return "Successfully added status"; } public static String timeline(String[] arg, User[] user, List&lt;User&gt; userslist) { if (arg.length != 1) return "Wrong number of parameters. TIMELINE doesn't take any parameters"; List&lt;Status&gt; Timeline = new ArrayList&lt;Status&gt;(); Timeline.addAll(user[0].getStats()); for (User l : user[0].getFriends()) Timeline.addAll(l.getStats()); Collections.sort(Timeline); String result = ""; for (Status s : Timeline) result += s.print() + "\n" + "Comments[" + s.getComments().size() + "]\n\n"; return result; } public static String exit(String[] arg, User[] user, List&lt;User&gt; userslist) { if (arg.length != 1) return "Wrong number of parameters. EXIT doesn't take any parameters"; if (user[0] != null) user[0].setCurrentlyLoggedIn(false); return "EXIT"; } } </code></pre> <p>Adding notifications</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload