Aenderungen Denis
This commit is contained in:
parent
fbb7ed2478
commit
5e805a4cc5
@ -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,9 +20,13 @@ 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 Client client = new Client();
|
private int port;
|
||||||
|
private JTextPane textPane;
|
||||||
|
private NetworkClient client;
|
||||||
|
|
||||||
|
// private Client client = new Client();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Launch the application.
|
* Launch the application.
|
||||||
*/
|
*/
|
||||||
@ -52,95 +54,103 @@ public class ClientGUI extends JFrame {
|
|||||||
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||||
setContentPane(contentPane);
|
setContentPane(contentPane);
|
||||||
contentPane.setLayout(null);
|
contentPane.setLayout(null);
|
||||||
|
|
||||||
userinput = new JTextField();
|
userinput = new JTextField();
|
||||||
userinput.setBounds(18, 88, 413, 28);
|
userinput.setBounds(18, 88, 413, 28);
|
||||||
contentPane.add(userinput);
|
contentPane.add(userinput);
|
||||||
userinput.setColumns(10);
|
userinput.setColumns(10);
|
||||||
|
|
||||||
JLabel lblMessage = new JLabel("Message:");
|
JLabel lblMessage = new JLabel("Message:");
|
||||||
lblMessage.setBounds(18, 60, 123, 16);
|
lblMessage.setBounds(18, 60, 123, 16);
|
||||||
contentPane.add(lblMessage);
|
contentPane.add(lblMessage);
|
||||||
|
|
||||||
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);
|
||||||
contentPane.add(lblServerResponse);
|
contentPane.add(lblServerResponse);
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
JLabel lblPort = new JLabel("Port:");
|
JLabel lblPort = new JLabel("Port:");
|
||||||
lblPort.setBounds(18, 26, 61, 16);
|
lblPort.setBounds(18, 26, 61, 16);
|
||||||
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);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,43 +9,53 @@ import java.util.Scanner;
|
|||||||
|
|
||||||
public class NetworkClient {
|
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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";
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user