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;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JTextArea;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JButton;
@ -22,9 +20,13 @@ public class ClientGUI extends JFrame {
private JPanel contentPane;
private JTextField userinput;
private JTextField portnumber;
//private Client client = new Client();
private JScrollPane scrollPane;
private int port;
private JTextPane textPane;
private NetworkClient client;
// private Client client = new Client();
/**
* Launch the application.
*/
@ -52,95 +54,103 @@ public class ClientGUI extends JFrame {
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
userinput = new JTextField();
userinput.setBounds(18, 88, 413, 28);
contentPane.add(userinput);
userinput.setColumns(10);
JLabel lblMessage = new JLabel("Message:");
lblMessage.setBounds(18, 60, 123, 16);
contentPane.add(lblMessage);
JButton btnPush = new JButton("Push");
btnPush.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//TODO
String text = "0" + userinput.getText();
//client.send(text);
System.out.println("Trying to push \""+ userinput.getText() + "\".");
try {
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);
JButton btnPop = new JButton("Pop");
btnPop.addActionListener(new ActionListener() {
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);
JTextPane textPane = new JTextPane();
textPane.setBounds(18, 228, 413, 113);
contentPane.add(textPane);
textPane = new JTextPane();
scrollPane = new JScrollPane(textPane);
scrollPane.setBounds(18, 228, 413, 113);
contentPane.add(scrollPane);
JLabel lblServerResponse = new JLabel("Server response:");
lblServerResponse.setBounds(18, 200, 123, 16);
contentPane.add(lblServerResponse);
portnumber = new JTextField();
portnumber.setBounds(54, 20, 61, 28);
portnumber.setText("9999");
portnumber.setText("1234");
contentPane.add(portnumber);
portnumber.setColumns(10);
JLabel lblPort = new JLabel("Port:");
lblPort.setBounds(18, 26, 61, 16);
contentPane.add(lblPort);
final JLabel serverInformation = new JLabel("");
serverInformation.setBounds(283, 20, 111, 28);
serverInformation.setBounds(300, 20, 130, 28);
contentPane.add(serverInformation);
JButton btnConnectToServer = new JButton("Connect to Server");
btnConnectToServer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int port = -1;
port = -1;
boolean portOkay = false;
try{
try {
port = Integer.parseInt(portnumber.getText());
if(1 > port || port > 65535 || portnumber.getText().isEmpty()){
if (1 > port || port > 65535
|| portnumber.getText().isEmpty()) {
throw new Exception();
}
serverInformation.setText("");
portOkay = true;
}
catch(Exception ex){
} catch (Exception ex) {
serverInformation.setForeground(Color.RED);
serverInformation.setText("invalid port!");
}
if(portOkay){
//TODO: connect to server.....
serverInformation.setForeground(Color.BLUE);
serverInformation.setText("connected");
if (portOkay) {
try {
client = new NetworkClient("localhost", port);
serverInformation.setForeground(Color.GREEN);
serverInformation.setText("connected!");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
btnConnectToServer.setBounds(127, 19, 144, 29);
btnConnectToServer.setBounds(127, 19, 165, 29);
contentPane.add(btnConnectToServer);
}
}

View File

@ -9,43 +9,53 @@ import java.util.Scanner;
public class NetworkClient {
private DatagramChannel channel;
private DatagramChannel channel;
private ByteBuffer buf;
private InetAddress inetaddr;
public NetworkClient(final String address, final int port) throws IOException {
private int port;
public NetworkClient(final String address, final int port)
throws IOException {
this.port = port;
inetaddr = InetAddress.getByName(address);
buf = ByteBuffer.allocate(1024);
channel = DatagramChannel.open();
channel.socket().connect(inetaddr, port);
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();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
new NetworkClient("localhost", 1234);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
buf.clear();
}
public void push(String text) throws IOException {
if (channel.socket().isConnected()) {
text = "0" + text;
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 (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";
}
}
}