简单的TCP聊天程序

imonkeyi 2018-01-11 07:35:46
没错误,但运行不对,不知道哪里出问题了
package cn.itcast.chapter11;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;

public class test1 extends JFrame implements ActionListener,WindowListener{

static final int PORT=6699;//服务端端口号
static final String IP="172.27.80.187";//服务端IP地址
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();//获取屏幕尺寸,使窗口居中
JTextArea area=new JTextArea();//文本域,用于放信息
JTextField tfield=new JTextField(45);//文本框,用于放要发的信息
JButton but=new JButton("确定");
JPanel panel=new JPanel();
JLabel label=new JLabel("信息");
static Socket client;

static {//先用服务端的IP地址和端口号实例化socket
try {
client=new Socket(IP,PORT);
}catch(Exception e) {
e.printStackTrace();
}
}

test1(){//构造函数,设置界面
setSize(640,420);
setLocation(screen.width/2-320,screen.height/2-210);
area.setLineWrap(true);
setTitle("客户端");
area.setEditable(false);

panel.add(label);
panel.add(tfield);
panel.add(but);

add(area,BorderLayout.CENTER);
add(panel,BorderLayout.SOUTH);
but.addActionListener(this);
tfield.addActionListener(this);

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setVisible(true);
}

public void actionPerformed(ActionEvent e) {//按确定时发信息给服务端并显示在文本域中
//String receiveMsg;
String sendMsg;
sendMsg=tfield.getText();
try {
//client=new Socket(IP,PORT);
//DataInputStream dis=new DataInputStream(client.getInputStream());
DataOutputStream dos=new DataOutputStream(client.getOutputStream());
if(sendMsg!="") {

String atext=area.getText();
String ftext="你发的信息"+tfield.getText()+"\r\n";
area.setText(atext+ftext);
dos.writeUTF(tfield.getText());
dos.close();
}
/*receiveMsg=dis.readUTF();

if(receiveMsg!="") {
area.setText(receiveMsg);
}


close(client);*/

}catch(Exception IO) {
IO.printStackTrace();
}

tfield.setText("");
}

/*public void close(Socket client)throws Exception {
client.getOutputStream().close();
client.getInputStream().close();
client.close();
}*/
public void receiveMsg() {//收信息函数,当有信息是显示在文本域中
String reMsg;
String Msg;
Msg=area.getText();
try {
InputStream is=client.getInputStream();
DataInputStream dis=new DataInputStream(is);
reMsg="接受到信息:"+dis.readUTF()+"\r\n";
if(reMsg!="") {
area.setText(Msg+reMsg);
}
is.close();
}catch(Exception e) {
e.printStackTrace();
}
}

public void windowClosing(WindowEvent e) {//关闭窗口时关闭socket
try {
client.close();
}catch(Exception e1) {
e1.printStackTrace();
}
Window window=e.getWindow();
window.dispose();
}
public void windowClosed(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}

public static void main(String[] args) {//主函数,实例化自己,调用收信息函数收信息
test1 test=new test1();
test.receiveMsg();
}
}
package cn.itcast.chapter11;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;

public class test3 extends JFrame implements ActionListener,WindowListener{

static final int PORT=6699;//定义服务端的端口号
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();//获取屏幕尺寸使窗口居中
JTextArea area=new JTextArea();
JTextField tfield=new JTextField(45);
JButton but=new JButton("确定");
JPanel panel=new JPanel();
JLabel label=new JLabel("信息");
static ServerSocket server;
static Socket client;

static {//静态代码块,先实例化serversocket并得到socket
try {
server=new ServerSocket(PORT);
client=server.accept();
}catch(Exception e) {
e.printStackTrace();
}
}

test3(){//构造函数,设置界面
setSize(640,420);
setLocation(screen.width/2-320,screen.height/2-210);
area.setLineWrap(true);
setTitle("服务端");
area.setEditable(false);

panel.add(label);
panel.add(tfield);
panel.add(but);

add(area,BorderLayout.CENTER);
add(panel,BorderLayout.SOUTH);
but.addActionListener(this);
tfield.addActionListener(this);

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setVisible(true);
}

public void actionPerformed(ActionEvent e) {//按确定时发信息给服务端并显示在文本域中
String receiveMsg;
String sendMsg;
sendMsg=tfield.getText();
try {
//server=new ServerSocket(PORT);
//Socket client=server.accept();
//DataInputStream dis=new DataInputStream(client.getInputStream());
DataOutputStream dos=new DataOutputStream(client.getOutputStream());
if(sendMsg!="") {

String atext=area.getText();
String ftext=tfield.getText()+"\r\n";
area.setText(atext+ftext);
dos.writeUTF(tfield.getText());

}
/*receiveMsg=dis.readUTF();

if(receiveMsg!="") {
area.setText(receiveMsg);
}
//close(client);*/
}catch(Exception IO) {
IO.printStackTrace();
}

tfield.setText("");
}
public void receiveMsg() {//收信息函数,当有信息是显示在文本域中
String reMsg,Msg;
Msg=area.getText();
try {
InputStream is=client.getInputStream();
DataInputStream dis=new DataInputStream(is);
reMsg="接受到信息:"+dis.readUTF()+"\r\n";
if(reMsg!="") {
area.setText(Msg+reMsg);
}
is.close();
}catch(Exception e) {
e.printStackTrace();
}
}

public void windowClosing(WindowEvent e) {//关闭窗口时关闭socket
try {
client.close();
server.close();
}catch(Exception e1) {
e1.printStackTrace();
}
Window window=e.getWindow();
window.dispose();
}
public void windowClosed(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
/*public void close(Socket client)throws Exception {
client.getOutputStream().close();
client.getInputStream().close();
client.close();
}*/
public static void main(String[] args) {//主函数,实例化自己,调用收信息函数收信息
test3 test=new test3();
test.receiveMsg();
}
}
...全文
459 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
tianfang 2018-01-12
  • 打赏
  • 举报
回复
先写好一端的代码,比如server端。然后使用telnet ip port方式进行测试,再去写客户端 两个教程: https://www.cise.ufl.edu/~amyles/tutorials/tcpchat/ http://surajzanvar.blogspot.com/p/echo-server-with-gui-in-java-client.html

3,405

社区成员

发帖
与我相关
我的任务
社区描述
专题开发/技术/项目 设计模式
社区管理员
  • 设计模式
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧