So my server sends information and receives information. I know this because i tested it on the local host. The problem is that the portion it receives comes out with little squares after the user input.
Here is a picture of the problem.
http://img706.imageshack.us/img706/3342/outputc.jpg
http://yfrog.com/jmoutputcj
Thank You
Server Code:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class UDPServer extends JPanel implements ActionListener {
protected JTextField textField;
protected JTextArea textArea;
private final static String newline = "\n";
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 DatagramSocket skt;
static InetAddress i;
static int portnumber;
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 {
IO();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void IO() throws IOException{
String text = textField.getText();
byte[] outbuf = new byte[1024];
int len = 1024;
byte message[] = text.getBytes();
DatagramPacket packet= new DatagramPacket(message, message.length,i /*skt.getInetAddress()*/, portnumber);
DatagramPacket packet2 = new DatagramPacket(outbuf, len);
textField.selectAll();
textArea.append("Server: "+text + newline);
skt.send(packet);
skt.receive(packet2);
String recmessage = new String(packet2.getData());
textArea.append("Client: "+recmessage + 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.");
Portnum=JOptionPane.showInputDialog("What port would you like to use?(4 Digits. Ex:4568)\n" +
"Tell this to the client.");
portnumber=Integer.parseInt(Portnum);
skt =new DatagramSocket(portnumber);
//ServerSocket serverSocket = new ServerSocket(portnumber);
//Socket skt =serverSocket.accept();
//In=new BufferedReader(new InputStreamReader(skt.getInputStream()));
//Out=new PrintWriter(skt.getOutputStream(),true);
createAndShowGUI();
}
}