Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 SocketException Error
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
SsJBebiGoku




PostPosted: Sat Mar 12, 2005 5:42 pm   Post subject: SocketException Error

Hi,

When i try outputting something through a socket a second time the following error comes up:
code:
java.net.SocketException: Software caused connection abort: socket write error


It appears at out.write() and/or out.flush() methods. I'm not sure why this is happening/how to fix it...

code:
String line;
        //BufferedReader in = new SafeBufferedReader(new InputStreamReader(System.in));
       
        try
        {
            while(true)
            {
                line = chatWindow.getMessage();
               
                if(line != null)
                {
                   if(line.equals("exit"))
                   {
                        out.write("Your opponent is disconnecting by request.\r\n");
                        out.flush();
                        break;
                    }
                    out.write(playerName + ": " + line + "\r\n");
                    out.flush();
                  //  break;
                }
            }
        }
        catch(IOException e){e.printStackTrace();}
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sat Mar 12, 2005 6:41 pm   Post subject: (No subject)

Can you show us your initialization of chatWindow?
rizzix




PostPosted: Sat Mar 12, 2005 8:04 pm   Post subject: (No subject)

maybe the stuff sent is not recieved on the other end, considering the Socket is a wrapper over the TCP protocol. What is "out" anyways? If its a buffered writer (or similar classes) it should not be a problem, i think. I've lost touch in network programming.
SsJBebiGoku




PostPosted: Tue Mar 15, 2005 10:46 pm   Post subject: (No subject)

Here's the whole "Output" class. it handles all the output sent from another computer. I've also posted the InputThread class as well that handles the input from the user and sends it out to another computer jsut for reference.

code:
class InputThread extends Thread
{
    InputStream in;
    GameBoard board;
    ChatWindow chatWindow;
    ArrayList characters;
   
    /**constructor with 2 parameter
     *@param anInputStream the input stream of the computer
     *@param board the board object to alter
     *@param aChat the chat window to communicate through
     */
    public InputThread(InputStream anInputStream,GameBoard board,ChatWindow aChat)
    {
        chatWindow = aChat;
        in = anInputStream;
        this.board = board;
        characters = new ArrayList();
    }
   
    /**overriding method to activate the thread
     */
    public void run()
    {
        int[] coords = new int[2];
        int j;
        String output = "";
        try
        {
            output = "";
            characters.clear();
            for(int i = 0;;i++)
            {
                j = in.read();
                if(j == 10)//line feed
                    break;

                characters.add(new Character((char)j));
                break;
            }
            for(int i = 0;i<characters.size();i++)
                output += characters.get(i).toString();
            chatWindow.println(output);
        }
        catch(SocketException e){}
        catch(IOException e){}
       
        try
        {
            in.close();
        }
        catch(IOException e){}
    }
}

/**Class/thread to handle the output streams
 */
class OutputThread extends Thread
{
    Writer out;
    String playerName;
    ChatWindow chatWindow;
   
    /**constructor with 2 parameters
     *@param anOutputStream the output stream of the computer
     *@param name the player's name
     *@param aChat the chat window to communicate through
     */
    public OutputThread(OutputStream anOutputStream, String name, ChatWindow aChat)
    {
        chatWindow = aChat;
        out = new OutputStreamWriter(anOutputStream);
        playerName = name;
    }
   
    /**overriding method to activate the thread
     */
    public void run()
    {
        String line;
        //BufferedReader in = new SafeBufferedReader(new InputStreamReader(System.in));
       
        try
        {
            while(true)
            {
                line = chatWindow.getMessage();
               
                if(line != null)
                {
                   if(line.equals("exit"))
                   {
                        out.write("Your opponent is disconnecting by request.\r\n");
                        out.flush();
                        break;
                    }
                    out.write(playerName + ": " + line + "\r\n");
                    out.flush();
                  //  break;
                }
            }
        }
        catch(IOException e){e.printStackTrace();}
       
        //System.out.println("Exited the system");
       
        try
        {
            //System.out.println("Closing");
            out.close();
        }
        catch(IOException e){}
    }
}


As you can see, ChatWindow is being initialized through the constructor. An object is being passed through the constructor from earlier on the program. I'm pretty sure it's not this that is causing the problem.
wtd




PostPosted: Tue Mar 15, 2005 11:15 pm   Post subject: (No subject)

Yeah. I'm just thinking that it might be the problem, and I still don't know if it's getting initialized, since a null reference could have easily been passed in as the argument aChat.

What I wouldn't give for some Eiffel-ish design by contract. Smile

code:
make(a_chat : CHAT_WINDOW) is
  require
    a_chat /= Void
  do
    -- ...
  end
SsJBebiGoku




PostPosted: Wed Mar 16, 2005 2:17 pm   Post subject: (No subject)

code:
private ChatWindow chatBox;
...
public SetupConnection(GameBoard theBoardObj, ChatWindow theChatBox)
    {
        chatFrame = new JFrame("Chat Box");
        chatBox = theChatBox;
        ------>board = theBoardObj;
        console = new SafeBufferedReader(new InputStreamReader(System.in));
        startConnect();
    }
...
Thread input = new InputThread(connection.getInputStream(),board,chatBox);
                    input.start();
                    Thread output = new OutputThread(connection.getOutputStream(), playerName,chatBox);
                    output.start();

it's definitely not the chat box that's the problem...it's weird, i can send 1 message from one user to the other, then when i attempt to send a second (or a first from the other user), the error comes up.
I've attached the code if you want to look at it



minedetection.zip
 Description:

Download
 Filename:  minedetection.zip
 Filesize:  35.66 KB
 Downloaded:  158 Time(s)

rizzix




PostPosted: Wed Mar 16, 2005 2:29 pm   Post subject: (No subject)

That actually is a much better explaination of your problem, now maybe we can help. You obviouly seem to be having some sort of multithreaded logic problems (ugh, the ugly ones).

First i need an overall view of your program design (logic and flow control).

I believe your program is a peer-to-peer chat program? So here the deal (unrelated but) keep this in mind, throughout ur program development: dont try and create swing components in another thread besides the main thread. For various reasons this is important including the fact that swing is not designed with multithreaded support. But it does provide ways to do the (multithreaded) stuff so long as its events and component creation/drawing are all in the same thread.

Next, from what little i've seen so far, your program creates two threads, one for input the other for output. Are they intreacting with swing components?

What does the getMessage() method of the chat window do? When does the tread quit? It seems that the chatWindow is used by both threads. This process has to be synchronized, i suggest u create a new Object() for this purpose. Well actually after futher analyzing.. i dont see the point of having threads in your code. Why do u need them?

The other thing is.. why not use the BufferedReader's readLine() method instead of read()?

If your program is solely based on character data transfer is suggest you take a look at the tutorial Networking: Sockets and ServerSockets, it uses a character based network io model. Take note of error checking etc.
SsJBebiGoku




PostPosted: Wed Mar 16, 2005 6:06 pm   Post subject: (No subject)

Alright, this is basically what happens:
a user types whatever message they want to send to the other user (yes, it is a P2P program), and once they click the "Send" button it sets the variable "msg" in the chatbox class to equal what the user typed. There is also a while(true) loop in the connection class that constantly checks if "msg" is equal to null; if it is not, then it writes it out, flushes the outputstream, etc. the getMessage() method is the method that returns "msg" from teh chatwindow class

the outputstream is that of the current user; it is this user's outputstream that is being used to send out the message to the other user. when "exit" is returned, then it basically tells the other user that the client has left (it's just for the other user's purpose)

i did not use readLine() because it is not "compatible" with all os's - with Windows, readLine() checks for a carriage return and a line feed (\r\n), for mac it checks for a line feed only(\n). My program isn't based on character tranfer, it is jsut because of the reason above that i used it.

Just to note, before creating a chat window and having all teh streams and everythign relate to the chat window (so basically communicating through it), i had the chat setup through the command prompt and it worked perfectly.
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Wed Mar 16, 2005 6:40 pm   Post subject: (No subject)

SsJBebiGoku wrote:
Alright, this is basically what happens:
a user types whatever message they want to send to the other user (yes, it is a P2P program), and once they click the "Send" button it sets the variable "msg" in the chatbox class to equal what the user typed. There is also a while(true) loop in the connection class that constantly checks if "msg" is equal to null; if it is not, then it writes it out, flushes the outputstream, etc. the getMessage() method is the method that returns "msg" from teh chatwindow class

the outputstream is that of the current user; it is this user's outputstream that is being used to send out the message to the other user. when "exit" is returned, then it basically tells the other user that the client has left (it's just for the other user's purpose)


Yep.. well. i dont see the point of the threads etc. etc.. Since, once the user clicks send, its best you just send the message! Don't stack it! That eliminates the unnecessary complication.

Although you might require a thread that loops continuously reading data from the other user and displaying it in the chatWindow. but for sending messges its not necessary.

Also i dont see whay it should concern you that windows uses "\r\n" as the line feed but unixes use "\n", cuz if this text is rendered using swing, swing will simply treat the "\r" as a whitespace character. Wouldn't it?
SsJBebiGoku




PostPosted: Fri Mar 18, 2005 10:35 am   Post subject: (No subject)

Thank you. that seems to have done the trick after playing aroudn with it a bit
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: