0

I thought UDP would allow sending and receiving concurrently which is why i switched to it. My TCP Client/Server does the same thing. So I'm basically back were i started, but i did learn some new stuff.

How would I setup the server and client to send/receive data concurrently?

0

Use a separate thread for the receive() method because it will block, ie not allow you to do anything until it has received a packet. The send() thread will be user driven: you enter a message and send() sends it.

0

When do i access the receive() method??
And the send method i can access after the user presses enter right?

0

Put the receive() method in its own thread. It does its own thing. Waits, receives a message, displays the message and then waits for the next one.
The send() would be called when the user presses enter asking for a message to be sent.

0

Ok I'm not sure what you mean by thread.

And if i do lets say,

Send();
recieve();

Won't java go to send and never get to recieve? The problem i believe is between lines 60-80.

Also please take a look at lines 107 & 108. and i call the Send() method as the action listener.

Here's my code so far for the server:

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.nio.charset.Charset;

import javax.swing.*;


public class UDPServer extends JPanel implements ActionListener {
	protected JTextField textField;
	protected static JTextArea textArea;
	private final static String newline = "\n";


	String ClientInput = null;
	String ServerInput=null;
	static String host=null;
	static String portnum=null;
	static BufferedReader In=null;
	static PrintWriter Out=null;
	static DatagramSocket skt;
	static InetAddress i;
	static int portnumber=4568;
	static DatagramPacket packet2;
	DatagramPacket packet;
	static byte[] outbuf = new byte[20];
	byte message[];

	public UDPServer() {
		super(new GridBagLayout());	

		textField = new JTextField(20);
		textField.addActionListener(this);

		textArea = new JTextArea(5, 20);
		textArea.setEditable(false);
		JScrollPane scrollPane = new JScrollPane(textArea);

		//Add Components to this panel.
		GridBagConstraints c = new GridBagConstraints();
		c.gridwidth = GridBagConstraints.REMAINDER;

		c.fill = GridBagConstraints.HORIZONTAL;
		add(textField, c);

		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1.0;
		c.weighty = 1.0;
		add(scrollPane, c);
	}

	public void actionPerformed(ActionEvent evt){
		try {
			Send();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	public void Send() throws IOException{
		String text = textField.getText();

		byte message[] = text.getBytes();
		
		packet= new DatagramPacket(message, message.length,packet2.getAddress(), portnumber);
				
		textField.selectAll();
		textArea.append("Outgoing: "+text + newline);
		skt.send(packet);		
	}
	private static void recieve() throws IOException {
		packet2 = new DatagramPacket(outbuf, outbuf.length);
		skt.receive(packet2);	
		skt.connect(packet2.getAddress(), portnumber);
		String packet2string=new String(outbuf,0,packet2.getLength(),"US-ASCII");
		textArea.append("Incoming: "+packet2string + newline);
		textArea.setCaretPosition(textArea.getDocument().getLength());		
	}

	private static void createAndShowGUI() {
		//Create and set up the window.
		JFrame frame = new JFrame("Chat Program Server");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//Add contents to the window.
		frame.add(new UDPServer());

		//Display the window.
		frame.pack();
		frame.setVisible(true);
	}

	public static void main(String[] args) throws UnknownHostException, IOException {

		i = InetAddress.getLocalHost();

		JOptionPane.showMessageDialog(null,"Your Ip Address is: "+i.getHostAddress()+
		"\n Give this to the Client.");


		skt = new DatagramSocket(portnumber);
		

		//Get the originating IP address of packet 2
		//Basically where packet 2 came from
		createAndShowGUI();
		recieve();
	}
}

Edited by Hunter2379: n/a

0

Have you done any research on Threads? You will need to read some of the Tutorial about how to use them. Without Threads, your code will always hand on the receive() method until data arrives.

Send();
recieve();

Won't java go to send and never get to recieve?

No, send() will be executed, the data will be send and then the program will continue to call receive() and wait there until it receives data. I'm assuming that the Send() and recieve() methods you show only do that and nothing more.

look at lines 107 & 108.

createAndShowGUI();
		recieve();

Why?

Edited by NormR1: n/a

0

So lets say i have a method named Send().

Would i do Thread send=new Send(); or Thread send=new Thread(Send);. I don't know how to do it. I'm also trying to find it on the api.

0

You need to read the Tutorial or some textbook before trying to use the Thread class. There are concepts you need to understand to be able to use threads.

0

Well I don't want to read a textbook.

You're not going to get far if you don't want to read a textbook

Do you ever expect to go to college?

Edited by NormR1: n/a

0

Yes. New update. I made the threads and they work. The only problem is that both server and client can only send and receive up to two msg's. After that you just can't send anything else. Also where is a good place to put the thread.start(); for my code? So it get's repeated a lot. Oh and the threads were built inside the main.

Server Code:

import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.*;


public class TextServer extends JPanel implements ActionListener {
	protected static JTextField textField;
	protected static JTextArea textArea;
	private final static String newline = "\n";


	static String ClientInput = null;
	static String Portnum=null;
	String ServerInput=null;
	static String host=null;
	static String portnum=null;
	static BufferedReader In=null;
	static PrintWriter Out=null;
	static Thread thread = null;
	static Thread thread2 = null;
	
	public TextServer() {
		super(new GridBagLayout());	

		textField = new JTextField(20);
		textField.addActionListener(this);

		textArea = new JTextArea(5, 20);
		textArea.setEditable(false);
		JScrollPane scrollPane = new JScrollPane(textArea);

		//Add Components to this panel.
		GridBagConstraints c = new GridBagConstraints();
		c.gridwidth = GridBagConstraints.REMAINDER;

		c.fill = GridBagConstraints.HORIZONTAL;
		add(textField, c);

		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1.0;
		c.weighty = 1.0;
		add(scrollPane, c);
	}

	public void actionPerformed(ActionEvent evt){
		thread.start(); 
		thread2.start();
	}
	/*public void Send() throws IOException{
		String text = textField.getText();			
		textArea.append("Server: "+text + newline);
		textField.selectAll();					
		Out.println(text);


	}*/



	private static void createAndShowGUI() {
		//Create and set up the window.
		JFrame frame = new JFrame("Chat Program Server");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//Add contents to the window.
		frame.add(new TextServer());

		//Display the window.
		frame.pack();
		frame.setVisible(true); 
	}

	public static void main(String[] args) throws UnknownHostException, IOException {

		InetAddress i = InetAddress.getLocalHost();

		JOptionPane.showMessageDialog(null,"Your Ip Address is: "+i.getHostAddress()+
		"\n Give this to the Client.");
		Portnum=JOptionPane.showInputDialog("What port would you like to use?(4 Digits. Ex:4568)\n" +
		"Tell this to the client.");
		int portnumber=Integer.parseInt(Portnum);	

		ServerSocket serverSocket = new ServerSocket(portnumber);
		Socket skt =serverSocket.accept();		

		In=new BufferedReader(new InputStreamReader(skt.getInputStream()));
		Out=new PrintWriter(skt.getOutputStream(),true);

		class BasicThread1 extends Thread {
			public void run() 
			{ 	
				try {
					ClientInput=In.readLine();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				textArea.append("Client: "+ClientInput + newline);
				textArea.setCaretPosition(textArea.getDocument().getLength());

			} 
		} 
		class BasicThread2 extends Thread {
			public void run() 
			{ 	
				String text = textField.getText();
				textArea.append("Client: "+text + newline);		
				textField.selectAll();		
				Out.println(text);				
			} 
		}
		// Move Recieve Thread (Thread) somewhere were it get's activated a lot.
		// Find a way to get more than 2 things posted.
		
		thread = new BasicThread1();
		thread2 = new BasicThread2();

		createAndShowGUI();
	}
}
0

problem is that both server and client can only send and receive up to two msgs

Why are you putting that restriction on the them?
Or what is causing that restriction?

0

I'm not sure. I was going to look through my code to find it. Let me know if you know what is causing it please.

0

Try debugging your code by adding some println() statements to see where the logic goes and what the values are at different places.
That should show you where and why the program behaves like it does.

0

It's finished. Thanks for all your help. I'm gonna customize it later.

0

ok now i want to make the chat work over the internet. Lets say my friends house is 99.75.32.14 and mines is 45.96.85.75. His private address is 192.168.1.100.

Can the socket address only be 99.75.32.14, or would i need to find a way to put his private ip address of 192.168.1.100 in the socket. If so, how do i do this?

0

i was looking for a code like this thank u

Edited by ROurOu: grammer

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.