Aenderungen Denis

This commit is contained in:
M.Scholz 2012-11-04 13:32:52 +01:00
parent fbb7ed2478
commit 5e805a4cc5
2 changed files with 100 additions and 80 deletions

View File

@ -1,15 +1,13 @@
package u1; package u1;
import java.awt.BorderLayout;
import java.awt.EventQueue; import java.awt.EventQueue;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
import javax.swing.JTextField; import javax.swing.JTextField;
import javax.swing.JTextPane; import javax.swing.JTextPane;
import javax.swing.JTextArea;
import java.awt.Color; import java.awt.Color;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JButton; import javax.swing.JButton;
@ -22,8 +20,12 @@ public class ClientGUI extends JFrame {
private JPanel contentPane; private JPanel contentPane;
private JTextField userinput; private JTextField userinput;
private JTextField portnumber; private JTextField portnumber;
private JScrollPane scrollPane;
private int port;
private JTextPane textPane;
private NetworkClient client;
//private Client client = new Client(); // private Client client = new Client();
/** /**
* Launch the application. * Launch the application.
@ -65,27 +67,37 @@ public class ClientGUI extends JFrame {
JButton btnPush = new JButton("Push"); JButton btnPush = new JButton("Push");
btnPush.addActionListener(new ActionListener() { btnPush.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
//TODO System.out.println("Trying to push \""+ userinput.getText() + "\".");
String text = "0" + userinput.getText(); try {
//client.send(text); client.push(userinput.getText());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
}); });
btnPush.setBounds(18, 128, 56, 44); btnPush.setBounds(18, 128, 70, 44);
contentPane.add(btnPush); contentPane.add(btnPush);
JButton btnPop = new JButton("Pop"); JButton btnPop = new JButton("Pop");
btnPop.addActionListener(new ActionListener() { btnPop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
try {
String received = client.pop();
textPane.setText(textPane.getText() + received);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} }
}); });
btnPop.setBounds(96, 128, 56, 44); btnPop.setBounds(100, 128, 70, 44);
contentPane.add(btnPop); contentPane.add(btnPop);
JTextPane textPane = new JTextPane(); textPane = new JTextPane();
textPane.setBounds(18, 228, 413, 113); scrollPane = new JScrollPane(textPane);
contentPane.add(textPane); scrollPane.setBounds(18, 228, 413, 113);
contentPane.add(scrollPane);
JLabel lblServerResponse = new JLabel("Server response:"); JLabel lblServerResponse = new JLabel("Server response:");
lblServerResponse.setBounds(18, 200, 123, 16); lblServerResponse.setBounds(18, 200, 123, 16);
@ -93,7 +105,7 @@ public class ClientGUI extends JFrame {
portnumber = new JTextField(); portnumber = new JTextField();
portnumber.setBounds(54, 20, 61, 28); portnumber.setBounds(54, 20, 61, 28);
portnumber.setText("9999"); portnumber.setText("1234");
contentPane.add(portnumber); contentPane.add(portnumber);
portnumber.setColumns(10); portnumber.setColumns(10);
@ -102,45 +114,43 @@ public class ClientGUI extends JFrame {
contentPane.add(lblPort); contentPane.add(lblPort);
final JLabel serverInformation = new JLabel(""); final JLabel serverInformation = new JLabel("");
serverInformation.setBounds(283, 20, 111, 28); serverInformation.setBounds(300, 20, 130, 28);
contentPane.add(serverInformation); contentPane.add(serverInformation);
JButton btnConnectToServer = new JButton("Connect to Server"); JButton btnConnectToServer = new JButton("Connect to Server");
btnConnectToServer.addActionListener(new ActionListener() { btnConnectToServer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
int port = -1; port = -1;
boolean portOkay = false; boolean portOkay = false;
try{ try {
port = Integer.parseInt(portnumber.getText()); port = Integer.parseInt(portnumber.getText());
if(1 > port || port > 65535 || portnumber.getText().isEmpty()){ if (1 > port || port > 65535
|| portnumber.getText().isEmpty()) {
throw new Exception(); throw new Exception();
} }
serverInformation.setText(""); serverInformation.setText("");
portOkay = true; portOkay = true;
} } catch (Exception ex) {
catch(Exception ex){
serverInformation.setForeground(Color.RED); serverInformation.setForeground(Color.RED);
serverInformation.setText("invalid port!"); serverInformation.setText("invalid port!");
} }
if(portOkay){ if (portOkay) {
try {
client = new NetworkClient("localhost", port);
//TODO: connect to server..... serverInformation.setForeground(Color.GREEN);
serverInformation.setText("connected!");
} catch (IOException e1) {
serverInformation.setForeground(Color.BLUE); // TODO Auto-generated catch block
serverInformation.setText("connected"); e1.printStackTrace();
}
} }
} }
}); });
btnConnectToServer.setBounds(127, 19, 144, 29); btnConnectToServer.setBounds(127, 19, 165, 29);
contentPane.add(btnConnectToServer); contentPane.add(btnConnectToServer);
} }
} }

View File

@ -12,40 +12,50 @@ public class NetworkClient {
private DatagramChannel channel; private DatagramChannel channel;
private ByteBuffer buf; private ByteBuffer buf;
private InetAddress inetaddr; private InetAddress inetaddr;
private int port;
public NetworkClient(final String address, final int port) throws IOException { public NetworkClient(final String address, final int port)
throws IOException {
this.port = port;
inetaddr = InetAddress.getByName(address); inetaddr = InetAddress.getByName(address);
buf = ByteBuffer.allocate(1024); buf = ByteBuffer.allocate(1024);
channel = DatagramChannel.open(); channel = DatagramChannel.open();
channel.socket().connect(inetaddr, port); channel.socket().connect(inetaddr, port);
buf.clear(); buf.clear();
while (channel.socket().isConnected()) {
Scanner sc = new Scanner(System.in);
String instr = sc.nextLine();
channel.send(ByteBuffer.wrap(instr.getBytes()), new InetSocketAddress(inetaddr, port));
channel.configureBlocking(false);
channel.receive(buf);
String received = new String(buf.array());
System.out.println(received);
buf.clear();
}
} }
/** public void push(String text) throws IOException {
* @param args if (channel.socket().isConnected()) {
*/ text = "0" + text;
public static void main(String[] args) { channel.send(ByteBuffer.wrap(text.getBytes()),
// TODO Auto-generated method stub new InetSocketAddress(inetaddr, port));
try { channel.configureBlocking(false);
new NetworkClient("localhost", 1234); channel.receive(buf);
} catch (IOException e) {
// TODO Auto-generated catch block String received = new String(buf.array());
e.printStackTrace(); System.out.println("Received (push): " + received);
buf.clear();
} else {
System.out.println("not connected anymore.");
} }
} }
public String pop () throws IOException {
if (channel.socket().isConnected()) {
String text = "1";
channel.send(ByteBuffer.wrap(text.getBytes()),
new InetSocketAddress(inetaddr, port));
channel.configureBlocking(false);
channel.receive(buf);
String received = new String(buf.array());
System.out.println("Received (pop): " + received);
buf.clear();
return received;
} else {
System.out.println("not connected anymore.");
return "fail";
}
}
} }