求大神指点,一个java小程序,做一个聊天工具,代码写出来了,没错但是就是运行不了,怎么办?
一下为代码package服务器端;importjavax.swing.*;importjava.io.*;importjava.net.*;importjava.awt....
一下为代码
package 服务器端;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
public class fuwuqi extends JFrame implements ActionListener
{
JTextArea jta=null;
JButton jb=null;
JTextField jtf=null;
JPanel jp=null;
JScrollPane jsp=null;
PrintWriter pw=null;
public static void main(String[] args)
{
fuwuqi l1331=new fuwuqi();
}
public fuwuqi()
{
jta=new JTextArea();
jb=new JButton("发送");
jb.addActionListener(this);
jtf=new JTextField(20);
jp=new JPanel(); jp.add(jtf); jp.add(jb);
jsp=new JScrollPane(jta);
this.add(jsp);
this.add(jp,"north");
this.setTitle("服务器端");
this.setSize(350,250);
this.setLocation(300,200);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
try{
ServerSocket ss=new ServerSocket(9165);
Socket s=ss.accept();
InputStreamReader isr=new InputStreamReader(s.getInputStream());
BufferedReader br=new BufferedReader(isr);
pw=new PrintWriter(s.getOutputStream(),true);
while(true)
{
String xinxi=br.readLine();
jta.append("客户端对我说: "+xinxi+"\r\n");//打印出来
}
}catch (Exception e){}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jb)
{
String xinxi=jtf.getText();
jta.append("我对客户端说: "+xinxi+"\r\n");
pw.println(xinxi);
jtf.setText("");}
}
}
} 展开
package 服务器端;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
public class fuwuqi extends JFrame implements ActionListener
{
JTextArea jta=null;
JButton jb=null;
JTextField jtf=null;
JPanel jp=null;
JScrollPane jsp=null;
PrintWriter pw=null;
public static void main(String[] args)
{
fuwuqi l1331=new fuwuqi();
}
public fuwuqi()
{
jta=new JTextArea();
jb=new JButton("发送");
jb.addActionListener(this);
jtf=new JTextField(20);
jp=new JPanel(); jp.add(jtf); jp.add(jb);
jsp=new JScrollPane(jta);
this.add(jsp);
this.add(jp,"north");
this.setTitle("服务器端");
this.setSize(350,250);
this.setLocation(300,200);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
try{
ServerSocket ss=new ServerSocket(9165);
Socket s=ss.accept();
InputStreamReader isr=new InputStreamReader(s.getInputStream());
BufferedReader br=new BufferedReader(isr);
pw=new PrintWriter(s.getOutputStream(),true);
while(true)
{
String xinxi=br.readLine();
jta.append("客户端对我说: "+xinxi+"\r\n");//打印出来
}
}catch (Exception e){}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jb)
{
String xinxi=jtf.getText();
jta.append("我对客户端说: "+xinxi+"\r\n");
pw.println(xinxi);
jtf.setText("");}
}
}
} 展开
3个回答
展开全部
给个简单的客户端的例子,lz对比自己的码看看有什么出入 (注意:ClientHandler是inner class)
package chap15;
import java.io.*;
import java.net.*;
import java.util.*;
public class VerySimpleChatServer
{
ArrayList clientOutputStreams;
public class ClientHandler implements Runnable {
BufferedReader reader;
Socket sock;
public ClientHandler(Socket clientSOcket) {
try {
sock = clientSOcket;
InputStreamReader isReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(isReader);
} catch (Exception ex) { ex.printStackTrace(); }
}
public void run() {
String message;
try {
while ((message = reader.readLine()) != null) {
System.out.println("read " + message);
tellEveryone(message);
}
} catch (Exception ex) { ex.printStackTrace(); }
}
}
public static void main(String[] args) {
new VerySimpleChatServer().go();
}
public void go() {
clientOutputStreams = new ArrayList();
try {
ServerSocket serverSock = new ServerSocket(5000);
while(true) {
Socket clientSocket = serverSock.accept();
PrintWriter writer = new PrintWriter(clientSocket.getOutputStream());
clientOutputStreams.add(writer);
Thread t = new Thread(new ClientHandler(clientSocket));
t.start();
System.out.println("got a connection");
}
} catch (Exception ex) { ex.printStackTrace(); }
}
public void tellEveryone(String message) {
Iterator it = clientOutputStreams.iterator();
while (it.hasNext()) {
try {
PrintWriter writer = (PrintWriter) it.next();
writer.println(message);
writer.flush();
} catch (Exception ex) { ex.printStackTrace(); }
}
}
}
package chap15;
import java.io.*;
import java.net.*;
import java.util.*;
public class VerySimpleChatServer
{
ArrayList clientOutputStreams;
public class ClientHandler implements Runnable {
BufferedReader reader;
Socket sock;
public ClientHandler(Socket clientSOcket) {
try {
sock = clientSOcket;
InputStreamReader isReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(isReader);
} catch (Exception ex) { ex.printStackTrace(); }
}
public void run() {
String message;
try {
while ((message = reader.readLine()) != null) {
System.out.println("read " + message);
tellEveryone(message);
}
} catch (Exception ex) { ex.printStackTrace(); }
}
}
public static void main(String[] args) {
new VerySimpleChatServer().go();
}
public void go() {
clientOutputStreams = new ArrayList();
try {
ServerSocket serverSock = new ServerSocket(5000);
while(true) {
Socket clientSocket = serverSock.accept();
PrintWriter writer = new PrintWriter(clientSocket.getOutputStream());
clientOutputStreams.add(writer);
Thread t = new Thread(new ClientHandler(clientSocket));
t.start();
System.out.println("got a connection");
}
} catch (Exception ex) { ex.printStackTrace(); }
}
public void tellEveryone(String message) {
Iterator it = clientOutputStreams.iterator();
while (it.hasNext()) {
try {
PrintWriter writer = (PrintWriter) it.next();
writer.println(message);
writer.flush();
} catch (Exception ex) { ex.printStackTrace(); }
}
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package 服务器端;
是什么意思,中文包?
是什么意思,中文包?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询