Strange socket problem. (chat program)

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1318 Views - Last Post: 08 December 2011 - 06:23 PM Rate Topic: -----

#1 Eqric  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 31-January 11

Strange socket problem. (chat program)

Posted 08 December 2011 - 02:49 AM

Hello guys, me and some friends are making a chat program and we get this strange problem.
We got 3 computers, lets call them A,B and C. A and B can connect and chat without problems. then we try A and C, they can connect but only C gets the messages and A get the error IOException at line 27 in the client code(see below)

this.out = new PrintStream(socket.getOutputStream(), true);

So it seems like the server are connected but it fails to get OutPutStream. We tried this on another computer and it didn't work there either.

The ports are open and there is no firewall blocking the connection.

Any idea on how to solve this?



The client code:
public class Client {
	private Socket socket = null;
	private PrintStream out = null;
	private Thread cThread = null;
	private InetAddress address = null;
	private SocketAddress SA;

	
	public Client(InetSocketAddress address) {

		System.out.println("Connecting client...");
		socket = new Socket();
		SA = address;
		try {
			socket.connect(SA, 0);
		} catch (IOException e) {
			//  Communication.Inlist.put(/*new SysMsg*/("Couldn't connect."));
			System.out.println("Cant connect.");
		}
		System.out.println("Client connected.");
		send();
	}

	
	public void send() {
		try {
			this.out = new PrintStream(socket.getOutputStream(), true);
			System.out.println("Writer.");
		} catch (IOException e) {
			System.out.println("Couldn't get outputStream");
		}
		cThread = new Thread(new clientThread());
		cThread.start();
	}
	
	public void stop() {
		cThread.interrupt();
	}
	
	private class clientThread implements Runnable{
		private String output;
		
		public clientThread() {
		}
		
		public void run() {
			while(true)
			{
				while ((output = Communication.Outlist.get()) != null) {
					//output = Communication.Outlist.get();
					out.println(output);
					/*if (output != null) {
					out.print(output);
				}*/
				}

			}
		}
	}
}


The server code:
public class Server {
	private ServerSocket serverSocket = null;
	private Thread sThread = null;

	
	public Server(int port){
		try {
			this.serverSocket = new ServerSocket(port);
		} catch (IOException e) {
			System.out.println("Could not listen on port.");
			e.printStackTrace();
		}
	}
	
	public void receive() {
		sThread = new Thread(new serverThread());
		sThread.start();
	}
	

	public void stop() {
		sThread.interrupt();
	}
	
	private class serverThread implements Runnable{
		private String input;
		private Socket socket;
		private BufferedReader in = null;

		
		public serverThread() {
		}
		
		public void run() {
			System.out.println("Server is listening.");
			try {
				this.socket = serverSocket.accept();
				System.out.println("Connected.");
			} catch (IOException e) {
				System.out.println("Could not accept.");
				e.printStackTrace();
			}
			try {
				this.in = new BufferedReader(
						new InputStreamReader(socket.getInputStream()));
			} catch (IOException e) {
				System.out.println("Could not create the reader.");
				e.printStackTrace();
			}
			try {
				while ((input = in.readLine()) != null) {
					//Communication.Inlist.put(input);
					Communication.Inlist.put(input);
					System.out.println(input);
					Mainwindow.temp = input;
				}
			} catch (IOException e) {
				System.out.println("Incoming failed.");
				e.printStackTrace();
			}
		}
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Strange socket problem. (chat program)

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Strange socket problem. (chat program)

Posted 08 December 2011 - 07:32 AM

Please when you post code and reference line number, include also your import statements so we can match the line number to give to us with the displayed code.

You have your server upside down, the server should not run not as a thread (no need for thread in the server listening the process is a simple loop)

create server socket
infinite_loop:
   accept client connect from server socket
   start thread to interact with that client
goto infinite_loop



Your client does not need a thread, unless you want a thread to monitor the inputStream and another thread doing something else like waiting for user input.
Was This Post Helpful? 0
  • +
  • -

#3 Eqric  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 31-January 11

Re: Strange socket problem. (chat program)

Posted 08 December 2011 - 07:36 AM

Quote

Please when you post code and reference line number, include also your import statements so we can match the line number to give to us with the displayed code.


Ok sry

Quote

Your client does not need a thread, unless you want a thread to monitor the inputStream and another thread doing something else like waiting for user input.

We have a GUI so we want to be able to write and get messages at the same time.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Strange socket problem. (chat program)

Posted 08 December 2011 - 07:48 AM

View PostEqric, on 08 December 2011 - 10:36 AM, said:

We have a GUI so we want to be able to write and get messages at the same time.

That's what I said, you need in your client a thread that monitors the input stream not one that
while ((output = Communication.Outlist.get()) != null) {

the thread is just:

while(true) {
  read from inputStream
  inform the GUI a message was received by calling a method and passing may be the message as parameter
}


Was This Post Helpful? 0
  • +
  • -

#5 Eqric  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 31-January 11

Re: Strange socket problem. (chat program)

Posted 08 December 2011 - 07:59 AM

Hmm, not quite sure what you mean, cause this is our first "larger" Java project.
Could that solve our "canīt get getOutputStream" issue?, or is it just a way to improve our code and make it more effcient?
Was This Post Helpful? 0
  • +
  • -

#6 The_Programmer-  Icon User is offline

  • Death Scythe
  • member icon

Reputation: 24
  • View blog
  • Posts: 593
  • Joined: 24-October 11

Re: Strange socket problem. (chat program)

Posted 08 December 2011 - 12:46 PM

I would store every single person that connects into an arraylist. It will make it easier to write to them.
Was This Post Helpful? 1
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Strange socket problem. (chat program)

Posted 08 December 2011 - 01:50 PM

View PostThe_Programmer-, on 08 December 2011 - 03:46 PM, said:

I would store every single person that connects into an arraylist. It will make it easier to write to them.

That's the way to go
You create a Client object from the Server and store it into an ArrayList
When the server has to broadcast it scans that ArrayList can call a method in the Client that writes on the output stream of the Server assigned to that Client

View PostEqric, on 08 December 2011 - 10:59 AM, said:

Hmm, not quite sure what you mean, cause this is our first "larger" Java project.
Could that solve our "canīt get getOutputStream" issue?, or is it just a way to improve our code and make it more effcient?

Its is more than being more efficient. Sorry but your architecture is kind of crooked :)
Was This Post Helpful? 0
  • +
  • -

#8 Eqric  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 31-January 11

Re: Strange socket problem. (chat program)

Posted 08 December 2011 - 02:00 PM

View Postpbl, on 08 December 2011 - 01:50 PM, said:

View PostThe_Programmer-, on 08 December 2011 - 03:46 PM, said:

I would store every single person that connects into an arraylist. It will make it easier to write to them.

That's the way to go
You create a Client object from the Server and store it into an ArrayList
When the server has to broadcast it scans that ArrayList can call a method in the Client that writes on the output stream of the Server assigned to that Client

View PostEqric, on 08 December 2011 - 10:59 AM, said:

Hmm, not quite sure what you mean, cause this is our first "larger" Java project.
Could that solve our "canīt get getOutputStream" issue?, or is it just a way to improve our code and make it more effcient?

Its is more than being more efficient. Sorry but your architecture is kind of crooked :)

Hehe ok, as i wrote before , this is our first "project" :) But for now, we just need a chat program that works between two computers.
The problem is that it works on some computers and with other not.
When itīs doesnīt work (most of the time) the socket get connected but one of the client failed to getOutputStream so just one person can send messages.
Was This Post Helpful? 0
  • +
  • -

#9 The_Programmer-  Icon User is offline

  • Death Scythe
  • member icon

Reputation: 24
  • View blog
  • Posts: 593
  • Joined: 24-October 11

Re: Strange socket problem. (chat program)

Posted 08 December 2011 - 02:35 PM

Do the clients need to open up a certain port? I think only the server has to open the port it's broadcasting on though.
Edit: Instead of "open" I meant "port forward."

This post has been edited by The_Programmer-: 08 December 2011 - 02:37 PM

Was This Post Helpful? 0
  • +
  • -

#10 Eqric  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 31-January 11

Re: Strange socket problem. (chat program)

Posted 08 December 2011 - 02:40 PM

View PostThe_Programmer-, on 08 December 2011 - 02:35 PM, said:

Do the clients need to open up a certain port? I think only the server has to open the port it's broadcasting on though.
Edit: Instead of "open" I meant "port forward."

Both need to have a specific port. The server listen on a certain port for incoming "sockets" and the client connect to a specific ip through a specific port
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Strange socket problem. (chat program)

Posted 08 December 2011 - 02:54 PM

Where is the code that:
- create your Server object
- call receive to start your ServerThread
Was This Post Helpful? 0
  • +
  • -

#12 Eqric  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 31-January 11

Re: Strange socket problem. (chat program)

Posted 08 December 2011 - 03:05 PM

This is our GUI code
public class MainWindow extends javax.swing.JFrame{

	public static String in;
	public static String ut;
	public static String temp;
	Security security = new Security();
	//Communication c = new Communication(8888);
	private String ip = "xxx.xx.xxx.xxx";
	public Thread sendThread;



        private void submitText() {
String get = jMsgArea.getText();
		get = " " + jUserName.getText() + " :\n" + get + "\n\n";
		chatHistory.append(get);
		//    MGP.player.start(FC);
		jMsgArea.setText("");
		Communication.Outlist.put(get);   //Communication.Outlist.put(get);
		ut = get;
	}

        private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
		
		submitText();
	}    

	
	public class updateChatHistory implements Runnable
	{
		public updateChatHistory()
		{

		}

		public void run()
		{
			while(true)
			{
				while( (temp = security.getMsg()) != null)
				{

					chatHistory.append("\n" + temp + "\n");
				}
			}
		}
	}
      

	}


import java.net.InetSocketAddress;
import java.util.*;

public class Communication {
	private int port = 0;
	public static FIFOlist Inlist = new FIFOlist(); // incoming
	public static  FIFOlist Outlist = new FIFOlist(); // outgoing

	private Server server;
	private Client client = null; //= new Client(IP, port);
	

	public Communication(int port) {
		server = new Server(port);
		this.port = port;
		startServer();
	}

	/
	public static void putIntoOutList(String test)
	{
		Outlist.put(test);

	}

	
	public void startServer() {
		server.receive();
	}
	

	public void connect(InetSocketAddress address) {
		this.client = new Client(address);
	}
	
	public void startClient() {
		client.send();
	}

	
	public void disconnect() {
		System.out.println("Disconnecting...");
		Outlist.put("&quit&");
		
		server.stop();
		client.stop();
		System.out.println("Disconnected.");
		server = new Server(port);
		startServer();

	}



	
}


Was This Post Helpful? 0
  • +
  • -

#13 Eqric  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 31-January 11

Re: Strange socket problem. (chat program)

Posted 08 December 2011 - 03:05 PM

*Edited: removed duplicated code

This post has been edited by pbl: 08 December 2011 - 06:18 PM

Was This Post Helpful? 0
  • +
  • -

#14 Eqric  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 31-January 11

Re: Strange socket problem. (chat program)

Posted 08 December 2011 - 03:09 PM

*Edited: removed duplicated code

This post has been edited by pbl: 08 December 2011 - 06:17 PM

Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Strange socket problem. (chat program)

Posted 08 December 2011 - 03:31 PM

	// OK called by Communication constructor
	public Server(int port){
		try {
			// creates server socket
			this.serverSocket = new ServerSocket(port);
	}
	
	// called after Object creation by Communication 
	public void receive() {
		sThread = new Thread(new serverThread());
		sThread.start();
	}
	
	
	private class serverThread implements Runnable{
		private String input;
		private Socket socket;
		private BufferedReader in = null;
		
		public void run() {
			System.out.println("Server is listening.");
			try {
				// OK socket contains the socket to communucate with the Client
				this.socket = serverSocket.accept();
				System.out.println("Connected.");
			try {
				// create input stream 
				this.in = new BufferedReader(
						new InputStreamReader(socket.getInputStream()));
			try {
				// OK loop forever here... who will listen do new connection requests ?
				while ((input = in.readLine()) != null) {
					//Communication.Inlist.put(input);
					Communication.Inlist.put(input);
					System.out.println(input);
					Mainwindow.temp = input;
				}
		}


Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2