Socket.java:(用户在单击我想在 server.java 上显示的按钮后写入任何文本)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GuiSocket extends JFrame implements Runnable{
JButton gonder;
JTextField yazi, gelen;
JPanel panel = new JPanel(new BorderLayout(2, 3));
Socket soket;
DataInputStream in;
DataOutputStream out;
Thread th;
public static void main(String[] args) throws IOException, Throwable {
GuiSocket gS = new GuiSocket();
}
public GuiSocket() throws IOException, Throwable {
this.setTitle("Socket - kibAr");
this.setBackground(Color.getHSBColor(120f, 100f, 50f));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(100, 100, 413, 253);
this.setLocationRelativeTo(getRootPane());
this.add(panel);
panel.setBackground(Color.RED);
hazirla();
th = new Thread(this);
th.start();
}
void ekranOrtala(){
this.setLocationRelativeTo(getRootPane());
}
void hazirla(){
yazi = new JTextField(50); panel.add(yazi, BorderLayout.WEST);
gonder = new JButton("Gönder"); panel.add(gonder, BorderLayout.EAST);
gelen = new JTextField(3); panel.add(gelen, BorderLayout.SOUTH);
this.pack();
ekranOrtala();
gonder.setFont(new Font("Tahoma", Font.BOLD, 10));
gonder.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
out = new DataOutputStream(soket.getOutputStream());
out.write(yazi.getText().getBytes());
yazi.setText("");
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
});
yazi.setFont (new Font("Consolas", Font.CENTER_BASELINE, 10));
gelen.setFont (new Font("Consolas", 10, 10));
gelen.setEditable(false);
gelen.setToolTipText("Sunucudan Gelen Mesaj");
gelen.setForeground(Color.RED);
yazi.setHorizontalAlignment((int) JTextField.CENTER_ALIGNMENT);
yazi.setForeground(Color.BLUE);
gonder.setToolTipText("Sunucuya Mesaj Gönder");
this.setVisible(true);
}
@Override
public void run() {
try {
soket = new Socket("127.0.0.1", 3131);
in = new DataInputStream(soket.getInputStream());
while(in.read() != -1){
gelen.setText(in.readUTF());
if(in.readUTF().charAt(0) == 'Ç'){
gelen.setText("Çıkış yapılıyor..");
System.exit(0);
}
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
服务器:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GuiServer extends JFrame implements Runnable {
JButton gonder;
JTextField yazi, gelen;
JPanel panel = new JPanel(new BorderLayout(2, 3));
Socket soket;
DataInputStream in;
DataOutputStream out;
Thread th;
public static void main(String[] args) throws Exception, Throwable {
GuiServer gS = new GuiServer();
}
public GuiServer() throws IOException, Throwable{
this.setTitle("Server - kibAr");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(100, 100, 413, 253);
this.setLocationRelativeTo(getRootPane());
this.add(panel);
panel.setBackground(Color.BLUE);
hazirla();
th = new Thread(this);
th.start();
}
void ekranOrtala(){
this.setLocationRelativeTo(getRootPane());
}
void hazirla(){
yazi = new JTextField(50); panel.add(yazi, BorderLayout.WEST);
gonder = new JButton("Gönder"); panel.add(gonder, BorderLayout.EAST);
gelen = new JTextField(3); panel.add(gelen, BorderLayout.SOUTH);
this.pack();
ekranOrtala();
gonder.setFont(new Font("Tahoma", Font.BOLD, 10));
gonder.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
out = new DataOutputStream(soket.getOutputStream());
out.write(yazi.getText().getBytes());
yazi.setText("");
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
});
yazi.setFont (new Font("Consolas", Font.CENTER_BASELINE, 10));
gelen.setFont (new Font("Consolas", 10, 10));
gelen.setEditable(false);
gelen.setToolTipText("Sunucudan Gelen Mesaj");
gelen.setForeground(Color.RED);
yazi.setHorizontalAlignment((int) JTextField.CENTER_ALIGNMENT);
yazi.setForeground(Color.BLUE);
gonder.setToolTipText("Sunucuya Mesaj Gönder");
this.setVisible(true);
}
@Override
public void run() {
ServerSocket sS;
try {
sS = new ServerSocket(3131);
soket = sS.accept();
--- Delete after work. > soket = new Socket("127.0.0.1", 3131);
in = new DataInputStream(soket.getInputStream());
while(in.read() != -1){
gelen.setText(in.readUTF());
if(in.readUTF().charAt(0) == 'Ç'){
gelen.setText("Çıkış yapılıyor..");
System.exit(0);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
当我在运行套接字后运行服务器时,我在单击“gonder”按钮后输入任何文本,但看不到我在服务器窗口上写的文本。
问题是什么?