Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    SirDagdlahan is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Problem with Multi-Threaded Socket IO

    So I've been writing some software on my own, more or less Warehouse Managerial needs.
    This is a general, overhead assessment on my end. I will be more descriptive after.
    The program has java clients, and a java server, where the java server (and only the server) interacts with a mysql server. More or less, it collects all the commands to be sent, and attempts to order them in a queue
    My problem seems to be that, despite my best efforts, I think things are still overlapping. Symptoms on the client side include not receiving full sends through a socket, or getting partial sends of data (This is all via tcp)

    To be more specific, the server has three main threads running: The first listens for new connections on the main port, and assigns them a second port of their own for interaction. The second thread (more of a thread type) is created along with the socket and listens for incoming messages from the client. The third thread reads from a queue when commands are added to it, as well as handles communication to the mysql server. The commands are added in the second thread, where there is a synchronized call to a method which adds a command, and the corresponding client id, to the queue. In the third thread, the command is sent to sql, and a response sent to the socket associated with the client id.

    My question is...is there something wrong with this ideology? Should the queue be made volatile? While many threads do access it, it is only accessed in a synchronized way, or so I think. If nothing can be thought of, I'll post code, but I feel with all the custom wrappings and such, it might only lead to more confusion.

    Thank you for anyone who takes to read this.

  2. #2
    SirDagdlahan is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: Problem with Multi-Threaded Socket IO

    I have been able to develop some code that accurately recreates the problem. Upon running it (If you set port forwarding, firewall settings etc for ports 25500-25520), The problem is simulated (simply by echoing) to recreate the problem I am facing, and in the same method as the code I have written

    For the client:
    Java Code:
    import java.io.IOException;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.Random;
    
    
    public class Connect1 {
    	public static void main(String[] args){
    		for(int i = 0; i < 20; i++){
    			Thread t = new Thread(){
    				private int port;
    				public void run(){
    					try {
    						Socket s = new Socket("IPHERE",25500);
    						byte[] in = new byte[1024];
    						int rd = s.getInputStream().read(in);
    						port = Integer.parseInt(new String(in, 0, rd));
    						if(port == 0) return;
    						s.close();
    						s = new Socket("IPHERE",port);
    						Random r = new Random();
    						int test = 1;
    						while(s.isConnected()){
    							String toSend = "";
    							for(int i = 0; i < 500; i++){
    								//48-122
    								toSend += (char)(r.nextInt(75) + 48);
    							}
    							
    							s.getOutputStream().write((toSend.getBytes().length + "").getBytes());
    							s.getOutputStream().flush();
    							s.getOutputStream().write(toSend.getBytes());
    							s.getOutputStream().flush();
    							int prelen = toSend.getBytes().length;
    							byte[] len = new byte[1024];
    							int length = s.getInputStream().read(len);
    							length = Integer.parseInt(new String(len, 0, length));
    							len = new byte[length];
    							int read = 0;
    							while(read != length)
    								read += s.getInputStream().read(len, read, length - read);
    							String red = new String(len);
    							System.out.println("Test" + test + " on port " + port + ": " + (red.equals(toSend)) + "," + (prelen == length) + ".");
    							if(!red.equals(toSend) || prelen != length)
    								throw new Exception("Not correct message");
    							test++;
    						}
    					} catch (Exception e) {
    						System.err.println("Error on Port: " + port);
    						e.printStackTrace();
    						System.exit(0);
    					}
    				}
    			};
    			try {
    				Thread.sleep(1000);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    			t.start();
    		}
    	}
    }
    And for the server:
    Java Code:
    import java.io.IOException;
    import java.net.Socket;
    import java.net.ServerSocket;
    import java.net.UnknownHostException;
    import java.util.ArrayList;
    
    
    public class Connect2 {
    	private volatile static Socket[] socks;
    	private volatile static ArrayList<Elem> map;
    	public static void main(String[] args){
    		try{
    			map = new ArrayList<Elem>();
    			Comm com = new Comm();
    			com.start();
    			socks = new Socket[20];
    			ServerSocket ss = new ServerSocket(25500);
    			while(true){
    				Socket s = ss.accept();
    				for(int i = 0; i < socks.length; i++){
    					Socket temp = socks[i];
    					if(temp == null || temp.isClosed()){
    						final String port = ("255" + ((i + 1) < 10 ? "0" + (i + 1) : (i + 1) + ""));
    						s.getOutputStream().write(port.getBytes());
    						s.getOutputStream().flush();
    						s.close();
    						ServerSocket tempss = new ServerSocket(Integer.parseInt(port));
    						final Socket fin = tempss.accept();
    						tempss.close();
    						socks[i] = fin;
    						fin.setSoTimeout(10000);
    						Thread t = new Thread(){
    							public void run(){
    								while(!fin.isClosed()){
    									try{
    										
    										byte[] lenbyte = new byte[1024];
    										int rd = fin.getInputStream().read(lenbyte);
    										int len = Integer.parseInt(new String(lenbyte, 0, rd));
    										lenbyte = new byte[len];
    										rd = 0;
    										while(rd != len)
    											rd += fin.getInputStream().read(lenbyte, rd, len - rd);
    										String red = new String(lenbyte);
    										enqueue(red, Integer.parseInt(port) - 25500);
    									}catch(IOException e){
    										try {
    											fin.close();
    										} catch (IOException e1) {
    											e1.printStackTrace();
    										}
    									}
    								}
    							}
    						};
    						t.start();
    						break;
    					}
    					else if(i == socks.length - 1){
    						s.getOutputStream().write("0".getBytes());
    						s.getOutputStream().flush();
    						s.close();
    					}
    				}
    			}
    		} catch (UnknownHostException e){
    			e.printStackTrace();
    		} catch (IOException e){
    			e.printStackTrace();
    		}
    	}
    	private static synchronized void enqueue(String red, int index) {
    		map.add(new Elem(red, index));
    	}
    	private static class Elem{
    		private String msg;
    		private int index;
    		public Elem(String msg, int index){
    			this.msg = msg;
    			this.index = index;
    		}
    		public int getIndex(){
    			return index;
    		}
    		public String getMsg(){
    			return msg;
    		}
    	}
    	private static class Comm extends Thread{
    		public void run(){
    			while(true){
    				try{
    					if(map.size() > 0){
    						Elem temp = map.remove(0);
    						Socket temps = socks[temp.getIndex() - 1];
    						temps.getOutputStream().write((temp.getMsg().getBytes().length + "").getBytes());
    						temps.getOutputStream().flush();
    						temps.getOutputStream().write(temp.getMsg().getBytes());
    						temps.getOutputStream().flush();
    					}
    				}catch(Exception e){
    					e.printStackTrace();
    				}
    			}
    		}
    	}
    }
    So...with this, I hope for more help from everyone. Thank you in advanced!

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,800
    Rep Power
    25

    Default Re: Problem with Multi-Threaded Socket IO

    The code doesn't have many debugging printlns to show its progress.
    How do you execute it for testing? Do you have a driver method that starts the classes?
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    SirDagdlahan is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: Problem with Multi-Threaded Socket IO

    You can add whatever you want, but there are a few debugs.
    The prints at the end show if the message that came back was the same, and if the lengths were the same: make sure the data transferred.
    The data sent is a 500 length string of random chars, from 48-122 (beginning lower to end of upper case, with some in-between)
    The server prints errors, as well as the client.
    What seems to happen is a mixture of two things: Either the client randomly receives an EOF, or the string message is received as the length.
    And both classes are run individually. Hence the main(String[] args)'s.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,800
    Rep Power
    25

    Default Re: Problem with Multi-Threaded Socket IO

    What happens when you use the localhost IP address?

    The server prints errors, as well as the client.
    Please post the full text of any error messages.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    SirDagdlahan is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: Problem with Multi-Threaded Socket IO

    Error on Port: 25517
    java.lang.NumberFormatException: For input string: "500Css27^mZ8jgtObXt<K?z0M\PeufKlbhiP6uCBgddFFDvFE AO33x...(Random characters: The 500 there shows it's getting combined..I can fix that)
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.parseInt(Integer.java:527)
    at Connect1$1.run(Connect1.java:35)

    Error on Port: 25507
    java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.checkBounds(String.java:406)
    at java.lang.String.<init>(String.java:578)
    at Connect1$1.run(Connect1.java:35)

    And on the server with that one
    java.lang.NullPointerException
    at Connect2$Comm.run(Connect2.java:95)

    I don't know why that one happens. The first I think is just sending the messages too quickly (happens on both server and client side. That one was client). Maybe I could have it sleep for a few milis beforehand.
    But the second one, the socket literally closes for no reason.

    Edit: I forgot to mention...that second error happens multiple times: Like a lot of the sockets are closing at once: Possibly all.
    Last edited by SirDagdlahan; 06-08-2012 at 12:16 AM. Reason: Addition of information

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,800
    Rep Power
    25

    Default Re: Problem with Multi-Threaded Socket IO

    java.lang.NumberFormatException: For input string: "500Css27^mZ8jgtObXt<K?
    You need to work on the protocol and not rely on when different parts of the message is read.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    SirDagdlahan is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: Problem with Multi-Threaded Socket IO

    What do you exactly mean by that? Are you talking about acknowledging when information is received, or...something else. I was under the assumption that sockets fully followed TCP/IP protocols, so ACK'ing wasn't necessary.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,800
    Rep Power
    25

    Default Re: Problem with Multi-Threaded Socket IO

    I was talking about the format of the data that was sent and how to separate the different parts of it.
    Part of your protocol is the sending of the port number. You need to design when and how the different pieces of data are sent. That is what I was calling a protocol. An agreement between your server and clients about how they are going to communicate.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    SirDagdlahan is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: Problem with Multi-Threaded Socket IO

    That wasn't the port number at all o.o
    All of the ports are between 25500-25520.
    And there is an agreement. First, there is the main connection on port 25500. After which, the server sends back the port to connect on for personal exchange. Then, a link is established between them on this secondary port. Messages are sent there. The sever blocks on read until a message is sent. The first thing received is the length of the string being sent: In this case that will always be 500. Next, after flushing, the string message itself is actually sent. The server has been reading since receiving the former message. After that, the same thing comes in reverse going back to the client (An echo)
    The 500 in front isn't a port, but the length of the string. Despite being sent at different times, and the flush occurring, the two messages are still being put together in the buffer on the receiving end. One possible way is to do an acknowledgement message after the initial one is sent with the size. That way, it knows it has already been read, and will then send the actual message once it gets the acknowledgement. I assumed that sending at different times, and flushing between, would deliver the messages separately. Apparently, that is not so.

    Also, that still leaves the problem of the one, and sometimes many, socket closes that randomly occur.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,800
    Rep Power
    25

    Default Re: Problem with Multi-Threaded Socket IO

    problem of the one, and sometimes many, socket closes that randomly
    Can you post debug print outs that shows that happening? Something that would allow others to test and get the same results.

    You can not rely on different parts of the message arriving at different times. That is why I suggested a record format or protocol to define what is being sent.

    BTW Hard coding 25500, and 255 at different places in the code makes it hard to convert / maintain.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    SirDagdlahan is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: Problem with Multi-Threaded Socket IO

    Can you post debug print outs that shows that happening? Something that would allow others to test and get the same results.
    The second and third error messages in my previous post. As I said, it is random. Keep running it, it will happen sometime.

    You can not rely on different parts of the message arriving at different times. That is why I suggested a record format or protocol to define what is being sent.
    Alright, I'l try the acknowledgement system I mentioned, as that would fit your criteria

    BTW Hard coding 25500, and 255 at different places in the code makes it hard to convert / maintain.
    As I said earlier, this recreates and simulates the problem. This is not the actual code from the program. I said earlier that the real code has far too many wrappers being used for all kinds of things. That's why I didn't post code in the first message. The methodology is exactly the same as what is being used. I simply use echoing instead of all the SQL that goes in instead. A solution to this is a solution to the real code.

    By the way, not trying to be rude, but some of your questions have been answered in previous posts.

    Thanks for your advice so far.

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,800
    Rep Power
    25

    Default Re: Problem with Multi-Threaded Socket IO

    The second and third error messages
    Sorry I don't see the connection with: socket closes that randomly
    StringIndexOutOfBoundsException: String index out of range: -1
    java.lang.NullPointerException
    I don't get those errors with the code I'm executing.

    This is the driver I'm using to test with:
    Java Code:
       final static String IPAddr = "localhost";    //<<<<<<<<<<<<<<<<<<<<<<<<<
       final static int ThePort = 8000;
       final static String ThePortPfx = "80";
    
       public static void main(final String[] args) {
    
          Thread t1 = new Thread(new Runnable() {
             public void run() {
                Server.main(args);
             }
          });
          t1.start();
          Thread t2 = new Thread(new Runnable() {
             public void run() {
                Client.main(args);
             }
          });
          t2.start();
    
          try{Thread.sleep(5000);}catch(Exception x){}
          System.exit(0);
       } // end main()
    This is the debug output from the program:


    Running: java MultiThreadSocket

    S - connected
    S - sending 8001
    S - waiting on port=8001
    C - rd=4 str=8001<
    C - connecting on port=8001
    S - connected port=8001
    S - out of for(i)
    C - sending=VEQLDJNZGYARIMRZXEVKJQELKGKTXN
    S - rd=32 str=30VEQLDJNZGYARIMRZXEVKJQELKGKTXN<
    enqueue red=VEQLDJNZGYARIMRZXEVKJQELKGKTXN, index=1
    C - Test1 on port 8001: true,true.
    C - sending=DQMOAVNZKHWTMVZYPOTXGJAPFLCAQG
    S - rd=32 str=30DQMOAVNZKHWTMVZYPOTXGJAPFLCAQG<
    enqueue red=DQMOAVNZKHWTMVZYPOTXGJAPFLCAQG, index=1
    C - Test2 on port 8001: true,true.
    C - sending=KVBOMIHKOCRXTPPOPMHUBHGRBACRUB
    S - rd=32 str=30KVBOMIHKOCRXTPPOPMHUBHGRBACRUB<
    enqueue red=KVBOMIHKOCRXTPPOPMHUBHGRBACRUB, index=1
    C - Test3 on port 8001: true,true.
    C - sending=HTAHCSAGDTLIIMHJFNTSZQGZJJAVSA
    S - rd=32 str=30HTAHCSAGDTLIIMHJFNTSZQGZJJAVSA<
    enqueue red=HTAHCSAGDTLIIMHJFNTSZQGZJJAVSA, index=1
    C - Test4 on port 8001: true,true.
    C - sending=BAOAMMDJROHMDLRSMKMFYHIOEWDDGG
    S - rd=32 str=30BAOAMMDJROHMDLRSMKMFYHIOEWDDGG<
    enqueue red=BAOAMMDJROHMDLRSMKMFYHIOEWDDGG, index=1
    C - Test5 on port 8001: true,true.
    C - sending=UFKWJPQRFIFFFCKUHGMCQPFXWVYAPR
    S - rd=32 str=30UFKWJPQRFIFFFCKUHGMCQPFXWVYAPR<
    enqueue red=UFKWJPQRFIFFFCKUHGMCQPFXWVYAPR, index=1
    C - Test6 on port 8001: true,true.
    C - sending=XKAONGOFIITTNLTFGYUVQCXCTNLTXH
    S - rd=32 str=30XKAONGOFIITTNLTFGYUVQCXCTNLTXH<
    enqueue red=XKAONGOFIITTNLTFGYUVQCXCTNLTXH, index=1
    C - Test7 on port 8001: true,true.
    C - sending=TLLFMPNJZDAMTAKQHTTLFVNAYCJLMG
    S - rd=32 str=30TLLFMPNJZDAMTAKQHTTLFVNAYCJLMG<
    enqueue red=TLLFMPNJZDAMTAKQHTTLFVNAYCJLMG, index=1
    C - Test8 on port 8001: true,true.
    C - sending=PWFZNGNIVAXDWNKWDLDGWSFKPJQLSA
    S - rd=32 str=30PWFZNGNIVAXDWNKWDLDGWSFKPJQLSA<
    enqueue red=PWFZNGNIVAXDWNKWDLDGWSFKPJQLSA, index=1
    C - Test9 on port 8001: true,true.
    C - sending=OUPSUZNHJTNWWJALOUVYCLQVMHNPZQ
    S - rd=32 str=30OUPSUZNHJTNWWJALOUVYCLQVMHNPZQ<
    enqueue red=OUPSUZNHJTNWWJALOUVYCLQVMHNPZQ, index=1
    C - Test10 on port 8001: true,true.
    C - sending=LWNDSPQRVYDSWEWCDIFWMKJLSDRDHD
    S - rd=32 str=30LWNDSPQRVYDSWEWCDIFWMKJLSDRDHD<
    enqueue red=LWNDSPQRVYDSWEWCDIFWMKJLSDRDHD, index=1
    C - Test11 on port 8001: true,true.
    C - sending=EDRDRTWHTCYSWUNTPECSPVLEQOQWVI
    S - rd=32 str=30EDRDRTWHTCYSWUNTPECSPVLEQOQWVI<
    enqueue red=EDRDRTWHTCYSWUNTPECSPVLEQOQWVI, index=1
    C - Test12 on port 8001: true,true.
    C - sending=ADDIBHEJGHNXEXKTVADPZXYMAHBZHN
    S - rd=32 str=30ADDIBHEJGHNXEXKTVADPZXYMAHBZHN<
    enqueue red=ADDIBHEJGHNXEXKTVADPZXYMAHBZHN, index=1
    C - Test13 on port 8001: true,true.
    C - sending=ZOZYXOGSLOPICFLBMECQGMATRHWPCQ
    S - rd=32 str=30ZOZYXOGSLOPICFLBMECQGMATRHWPCQ<
    enqueue red=ZOZYXOGSLOPICFLBMECQGMATRHWPCQ, index=1
    C - Test14 on port 8001: true,true.
    C - sending=OGMGKGABIINLAJCPJCSUEJUAJMGKCQ
    S - rd=32 str=30OGMGKGABIINLAJCPJCSUEJUAJMGKCQ<
    enqueue red=OGMGKGABIINLAJCPJCSUEJUAJMGKCQ, index=1
    C - Test15 on port 8001: true,true.
    C - sending=GTJFFVAUTPLBYANKTCRZMHDYWTNGKA
    S - rd=32 str=30GTJFFVAUTPLBYANKTCRZMHDYWTNGKA<
    enqueue red=GTJFFVAUTPLBYANKTCRZMHDYWTNGKA, index=1
    C - Test16 on port 8001: true,true.
    C - sending=ONDZOKPAUBHQUXWNWECCSJYSKPMLUD
    S - rd=32 str=30ONDZOKPAUBHQUXWNWECCSJYSKPMLUD<
    enqueue red=ONDZOKPAUBHQUXWNWECCSJYSKPMLUD, index=1
    C - Test17 on port 8001: true,true.
    C - sending=WQJRUCHBIFJKUIXIFMBNXRJCKJJVMD
    S - rd=32 str=30WQJRUCHBIFJKUIXIFMBNXRJCKJJVMD<
    enqueue red=WQJRUCHBIFJKUIXIFMBNXRJCKJJVMD, index=1
    C - Test18 on port 8001: true,true.
    C - sending=CPGMQFXLGELOOAGNHESKVYSIKOLUWG
    S - rd=32 str=30CPGMQFXLGELOOAGNHESKVYSIKOLUWG<
    enqueue red=CPGMQFXLGELOOAGNHESKVYSIKOLUWG, index=1
    C - Test19 on port 8001: true,true.
    C - sending=JKSQLAYCSYQVFEPXOWSHEWQVLEVJHH
    S - rd=32 str=30JKSQLAYCSYQVFEPXOWSHEWQVLEVJHH<
    enqueue red=JKSQLAYCSYQVFEPXOWSHEWQVLEVJHH, index=1
    C - Test20 on port 8001: true,true.
    C - sending=ONXCWGTDJAENKICSORYLQKVZAVQJAX
    S - rd=32 str=30ONXCWGTDJAENKICSORYLQKVZAVQJAX<
    enqueue red=ONXCWGTDJAENKICSORYLQKVZAVQJAX, index=1
    C - Test21 on port 8001: true,true.
    C - sending=ZKETBPOWYGGBEMQHRXEHAQBRVYGPQZ
    S - rd=32 str=30ZKETBPOWYGGBEMQHRXEHAQBRVYGPQZ<
    enqueue red=ZKETBPOWYGGBEMQHRXEHAQBRVYGPQZ, index=1
    C - Test22 on port 8001: true,true.
    C - sending=UPQTLDQOMADYKCUNRUSXGDCVVVMRPY
    S - rd=32 str=30UPQTLDQOMADYKCUNRUSXGDCVVVMRPY<
    enqueue red=UPQTLDQOMADYKCUNRUSXGDCVVVMRPY, index=1
    C - Test23 on port 8001: true,true.
    C - sending=LYSCZTWFIFFVVVFWBRGBAMPFMNVTTK
    S - rd=2 str=30<
    enqueue red=ex=1
    S - rd=30 str=LYSCZTWFIFFVVVFWBRGBAMPFMNVTTK<
    enqueue red=SCZTWFIFFVVVFWBRGBAMPFMNVTTK
    0 error(s)
    Last edited by Norm; 06-08-2012 at 03:21 AM.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    SirDagdlahan is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: Problem with Multi-Threaded Socket IO

    You didn't need to make a driver class. If you literally copy and paste the code, running it on two separate machines, it works fine.
    (I should probably note that: This isn't mean to be tested in localhost)
    Also, I'd say It ran through ~ 100 tests before an error occurred.

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,800
    Rep Power
    25

    Default Re: Problem with Multi-Threaded Socket IO

    I missed where you said it took 100 tests before the problem occurs.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    positimothy is offline Member
    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0

    Default Re: Problem with Multi-Threaded Socket IO

    Hey SirDagdlahan,

    I have exactly the same problem.

    I wrote a client server app that writes messages between the client & server over a socket with printwriters and bufferedreaders and I implemented a couple of methods that transfer files between the client & server over the same socket using file streams and buffered input/output streams. When I send files to the server from the client I never have problems. EVER. NEVER. When I send files from the server to the client, I have these problems of completely random mixups, exceptions or hangs. Messages getting received but then written to the file or file contents being received as messages. Sometimes things will work 10 times and then get these problems. The exception is always an indexOoutOfBoundsError writing the file. The receiving end reads -1 when the other side wasn't even closed yet and hadn't written all the bytes and in fact continues to send ALL the file bytes so how can the socket or stream be closed if it still writes to it?

    The file transfer first sends the file name and then size (these messages NEVER go missing) & only that many bytes is sent/received so I don't see how this could cause confusion. The printwriters & bufferedreaders read and write lines with end of file delimiters \n, \r \n\r or whatever so again, I don't understand how this could cause problems.

    I put in prints before and after the file transfer and the messages being sent and messages don't go missing AS OFTEN. It still happens but it takes many many more runs for problems to occur & when it does, what usually happens is both the client and server will receive the filename & file size and then just sit there waiting instead of sending the file bytes. The exception doesn't happen anymore but the writing of filebytes as messages does happen sometimes.

    I'm perversely happy to to have found this post because I've been searching for a couple of weeks & I just cannot understand what is going on.
    Last edited by positimothy; 06-09-2012 at 01:50 AM. Reason: clarification

  17. #17
    positimothy is offline Member
    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0

    Default Re: Problem with Multi-Threaded Socket IO

    Just to add some further information. These problems I'm having occur whether the client and server are running on the same machine or not.

    Also I am creating threads for each server, but I never create more than one client server pair at a time because I haven't automated this aspect of testing. So at each end; server and client, there is only a single thread reading & writing over the socket.
    Last edited by positimothy; 06-09-2012 at 01:57 AM.

  18. #18
    SirDagdlahan is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: Problem with Multi-Threaded Socket IO

    A quick thought: Despite setting bypasses in the firewall, as is done with any I/O program, is there another thing to 'deactivate', maybe something in windows itself? After some more testing in different methods, the problems only seem to occur when too many things happen at once. At this point, it simply refuses all requests and messages, or so it seems.

  19. #19
    positimothy is offline Member
    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0

    Default Re: Problem with Multi-Threaded Socket IO

    Quote Originally Posted by SirDagdlahan View Post
    A quick thought: Despite setting bypasses in the firewall .., maybe something in windows itself? ... the problems only seem to occur when too many things happen at once. At this point, it simply refuses all requests and messages, or so it seems.
    I'm running linux and have all firewall features turned off completely.

  20. #20
    SirDagdlahan is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: Problem with Multi-Threaded Socket IO

    After more testing, I've concluded that the problem seems to be in the approach.
    TCP/IP is definitely a good protocol, but not a perfect one. Relying on a constantly connected socket and expecting it to stay that way seems to be the folly of my design.
    My final solution turns to an approach that seems to be almost too simplistic.
    Refer back to the first server you ever did, something like a one client echo and reply. You connected, and disconnected after you were done. This seems to be the best approach. I have dedicated two ports on my computer for communication: One handles incoming queries for the database, while the other handles new connections and heartbeats. I only have sockets connected when there is something to send, and that's it. The server constantly holds on accept until something is reached while the client only opens the socket when it has information to send. The great part of this is that while using try and catch for the connecting, sending, and receiving, I have an easy way to restart the method if an error occurs: Just restart the method from the catch clause. If anyone wants my sample code I'll post it, but the solution is more easily followed in the "Clock echo" example SUN provides. Thank you to everyone who lent a hand.
    Last edited by SirDagdlahan; 06-13-2012 at 10:57 PM. Reason: spelling

Page 1 of 2 12 LastLast

Similar Threads

  1. JAVA web service (XML-RPC) is it multi threaded?
    By Rickyp in forum Advanced Java
    Replies: 1
    Last Post: 07-07-2011, 07:28 AM
  2. Implement a multi-threaded matrix multiplication program.
    By redasu in forum Threads and Synchronization
    Replies: 0
    Last Post: 11-17-2010, 10:00 AM
  3. Writing into a File in a Multi Threaded app.
    By snehalkgandhi in forum Threads and Synchronization
    Replies: 1
    Last Post: 12-19-2008, 06:10 AM
  4. Replies: 0
    Last Post: 07-22-2008, 05:39 PM
  5. Replies: 0
    Last Post: 04-07-2008, 09:14 PM

Tags for this Thread

Posting Permissions

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