Very simple chat program hanging
This is the code. it seems to be a problem with the threads not the socket connection, when i run it it just uses all the cpu and hangs Help me please i`m loosing my mind over this coz i can`t really find something wrong with it and it is due tomorrow...
Code java:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package chat;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
/**
*
* @author taghack
*/
class Read extends Thread
{
BufferedReader in;
public Read(BufferedReader in)
{
this.in=in;
}
@Override
public void run()
{
try
{
while(true)
{
if(in.ready())
{
try
{
//byte[] tmparr=new byte[100000];
//in.read(tmparr);
System.out.println(in.readLine());
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
class Write extends Thread
{
PrintWriter out;
public Write(PrintWriter out)
{
this.out=out;
}
@Override
public void run()
{
//byte[] tmparr=new byte[100000];
while(true)
{
if(Main.consoleinput.hasNext())
{
try
{
String message;
message=Main.consoleinput.next();
out.println(message);
out.flush();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
}
public class Main {
public static String nickname;
public static Scanner consoleinput=new Scanner(System.in);
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try
{
// TODO code application logic here
System.out.println("Please input your nickname:");
nickname=consoleinput.nextLine();
System.out.println("Please input the ip of the other participant in the chat session");
String ip=consoleinput.nextLine();
boolean surverisrunning=false;
Socket mainsocket;
int numberoftries=0;
do
{
try
{
numberoftries++;
System.out.println("trying to connect "+numberoftries);
Socket temp=new Socket(ip,3000);
surverisrunning=true;
temp.close();
}
catch(Exception e)
{
surverisrunning=false;
e.printStackTrace();
}
}
while(surverisrunning==false && numberoftries<10);
if(surverisrunning)
{
mainsocket=new Socket(ip,3000);
System.out.println("CONNECTION ESTABLISHED AS CLIENT!");
}
else
{
System.out.println("PleaseWainForConnctionFromClient");
ServerSocket a=new ServerSocket(3000);
mainsocket=a.accept();
System.out.println("CONNECTION ESTABLISHED AS SERVER!");
surverisrunning=true;
}
BufferedReader in=new BufferedReader(new InputStreamReader(mainsocket.getInputStream()));
PrintWriter out=new PrintWriter(new OutputStreamWriter(mainsocket.getOutputStream()));
Read read1=new Read(in);
Write write1=new Write(out);
read1.start();
write1.start();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Re: Very simple chat program hanging
Read the error messages....
Code :
Connection refused: connect
trying to connect 2
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at chat.Main.main(Main.java:103)
trying to connect 3
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at chat.Main.main(Main.java:103)
java.net.ConnectException: Connection refused: connect
Quote:
The exception says that there is no listening socket at the address
and port number you attempted to connect to.
Is the process running? Is it listening where you think it is? Are you
connecting where you think you are? Most of these things can be
diagnosed with e.g. netstat on the target machine and a network
snooping tool like tcpdump or ethereal.
java.net.ConnectException: Connection refused
Re: Very simple chat program hanging
well i know about that first set of 10 errors they are simply a set of 10 attempts to connect to a server to see if there si one running on the target machine and if not the code goes on to establish a rurver itsself so that is not a problem. i am pretty sure that the two isntances of the program (currently testing one one pc running two times) do connest to each other the problem seems to come from the threads maybe tey are in sone sort of weird deadlock (well not exactly) but i cant seem to figure it out
Re: Very simple chat program hanging
Quote:
Originally Posted by macko
Read the error messages....
The original poster did not mention this exception, and given you don't have the ip of the server you should not assume this is related to the problem at hand.
taghack, please wrap your code in the code tags (I edited your post for you). Second
Quote:
Originally Posted by taghack
when i run it it just uses all the cpu and hangs
Hangs where? Add some println's in there to determine where it hangs to debug the flow of the code. We don't have a server to connect to, so if your program hangs beyond that point you need to give us more information, such as where it 'hangs', what the server sends, etc...
Re: Very simple chat program hanging
thanks for the edit (not familiar with the forum sry) the connection seems to play out just fine
System.out.println("CONNECTION ESTABLISHED AS CLIENT!");
System.out.println("CONNECTION ESTABLISHED AS SERVER!");
these two messages are right after the attempts to connect the two sockets together and should only display if the connection is ok and they do in their respective instances of the program so i really think its a matter of threads. otherwise it mamages to start the two threads after which netbeans hangs from lack of cpu no error code no nothing
Re: Very simple chat program hanging
If the server is online can you connect via the client?
If the client doesn't connect when the servers off ?
Re: Very simple chat program hanging
well the idea is that the program can be both a client and a server it checks it there is a server on the other machine( the 10 errors) and if not opens a server socket itsself and waits for a connection but i think that that part is actually working well
Re: Very simple chat program hanging
The issue is still at hand going to rest now but anny suggestion will be greatly appreciated ! Thank you all VERY VERY much for your help !