这是一个聊天系统:调用sendStr()的数据写到服务器端,怎么服务器端没有显示?是空白的?
我测试过了,服务器端没问题,我是在jtable表的监听类中调用createJdialog()的,这个sendStr()方法,怎么传递这个socket和DataOutputStream ,不知道哪里有错?关键代码如下:这是一个表格的鼠标监听类:
jtable.addMouseListener(new MouseListerers()); class MouseListerers extends MouseAdapter {
JTable table = null;
private int row ,column;
private String name;
public void mousePressed(MouseEvent e) {
table = (JTable)e.getSource();
row =table.getSelectedRow();
column = table.getSelectedColumn();
name = "与"+table.getValueAt(row,column).toString()+"进行聊天中....";
if (e.getClickCount()==2 ) {
new ClientDemo().createJdialog(name); // 调用一个对话框 方法如下: }
}
}这是另外一个类的createJdialog()方法: public void createJdialog(String title){
JFrame jf = new JFrame();
jtra = new JTextArea(jf.getSize().width,jf.getSize().height);
jtf = new JTextField(jf.getSize().width);
jf.setTitle(title);
jf.add(jtra,BorderLayout.CENTER);
jf.add(jtf,BorderLayout.SOUTH);
jf.setBackground(Color.WHITE);
jf.setLocation(200,150);
jf.setSize(350, 300);
jtf.addActionListener(new TFlistener());
jf.setVisible(true);
jf.addWindowListener(new WindowAdapter(){
//@ Override
public void windowClosing(WindowEvent args0){
disConnct(); // 关闭连接
System.exit(0);
}
});
}
class TFlistener implements ActionListener{ public void actionPerformed(ActionEvent e) {
ss = jtf.getText().trim();
jtra.setText(ss);
jtf.setText("");
sendStr(ss,ClientDemo.this.getS(), // 主要 这个方法出现错误:
ClientDemo.this.getDos());
}
}// 建立连接方法
public boolean connect(){
boolean sboolean = false;
try {
s = new Socket("127.0.0.1",2000);
dos = new DataOutputStream(s.getOutputStream());
sboolean = true;
this.setS(s);
this.setDos(dos);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sboolean;
}
// 写数据
public void sendStr(String strDos,Socket soc,DataOutputStream dosm){ // 传递的参数
try {
dosm.writeUTF(strDos); // eclipse 报这行错误,java.lang.NullprointException
dosm.flush();
} catch (IOException e1) {
System.out.println(e1);
}
}
public DataOutputStream getDos() {
return dos;
} public void setDos(DataOutputStream dos) {
this.dos = dos;
} public Socket getS() {
return s;
} public void setS(Socket s) {
this.s = s;
}服务器端:private void serverStart() {
boolean started = false;
try {
serSocket = new ServerSocket(8888);
started = true;
jtextAr.append("服务器已启动 ! 正等待客户端连接......") ;
jtextAr.append("\12");
while(started){
boolean bConnected = false;
Socket socket = serSocket.accept();
bConnected = true;
jtextAr.append("一个客户端已经连接上了!") ;
jtextAr.append("\12");
while (bConnected){
dis = new DataInputStream(socket.getInputStream());
String sutf = dis.readUTF();
jtextAr.append(sutf);
jtextAr.append("\12");
}
dis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
我测试过了,服务器端没问题,我是在jtable表的监听类中调用createJdialog()的,这个sendStr()方法,怎么传递这个socket和DataOutputStream ,不知道哪里有错?关键代码如下:这是一个表格的鼠标监听类:
jtable.addMouseListener(new MouseListerers()); class MouseListerers extends MouseAdapter {
JTable table = null;
private int row ,column;
private String name;
public void mousePressed(MouseEvent e) {
table = (JTable)e.getSource();
row =table.getSelectedRow();
column = table.getSelectedColumn();
name = "与"+table.getValueAt(row,column).toString()+"进行聊天中....";
if (e.getClickCount()==2 ) {
new ClientDemo().createJdialog(name); // 调用一个对话框 方法如下: }
}
}这是另外一个类的createJdialog()方法: public void createJdialog(String title){
JFrame jf = new JFrame();
jtra = new JTextArea(jf.getSize().width,jf.getSize().height);
jtf = new JTextField(jf.getSize().width);
jf.setTitle(title);
jf.add(jtra,BorderLayout.CENTER);
jf.add(jtf,BorderLayout.SOUTH);
jf.setBackground(Color.WHITE);
jf.setLocation(200,150);
jf.setSize(350, 300);
jtf.addActionListener(new TFlistener());
jf.setVisible(true);
jf.addWindowListener(new WindowAdapter(){
//@ Override
public void windowClosing(WindowEvent args0){
disConnct(); // 关闭连接
System.exit(0);
}
});
}
class TFlistener implements ActionListener{ public void actionPerformed(ActionEvent e) {
ss = jtf.getText().trim();
jtra.setText(ss);
jtf.setText("");
sendStr(ss,ClientDemo.this.getS(), // 主要 这个方法出现错误:
ClientDemo.this.getDos());
}
}// 建立连接方法
public boolean connect(){
boolean sboolean = false;
try {
s = new Socket("127.0.0.1",2000);
dos = new DataOutputStream(s.getOutputStream());
sboolean = true;
this.setS(s);
this.setDos(dos);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sboolean;
}
// 写数据
public void sendStr(String strDos,Socket soc,DataOutputStream dosm){ // 传递的参数
try {
dosm.writeUTF(strDos); // eclipse 报这行错误,java.lang.NullprointException
dosm.flush();
} catch (IOException e1) {
System.out.println(e1);
}
}
public DataOutputStream getDos() {
return dos;
} public void setDos(DataOutputStream dos) {
this.dos = dos;
} public Socket getS() {
return s;
} public void setS(Socket s) {
this.s = s;
}服务器端:private void serverStart() {
boolean started = false;
try {
serSocket = new ServerSocket(8888);
started = true;
jtextAr.append("服务器已启动 ! 正等待客户端连接......") ;
jtextAr.append("\12");
while(started){
boolean bConnected = false;
Socket socket = serSocket.accept();
bConnected = true;
jtextAr.append("一个客户端已经连接上了!") ;
jtextAr.append("\12");
while (bConnected){
dis = new DataInputStream(socket.getInputStream());
String sutf = dis.readUTF();
jtextAr.append(sutf);
jtextAr.append("\12");
}
dis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
解决方案 »
- 一个IO问题
- java.lang.NoSuchMethodError 达人们出手相救阿!
- 打开一个模态JDialog,怎么才让父窗体在dialog关闭后才得到其返回值
- 请教配如下配置后就找不到ORB类了?为什么?
- 郁闷!散分!小的们,接分了!!!
- 如何显示url中的内容?就象ie一样,在appl中,调用默认浏览器也行
- 人生重大转折!!!请高手给予指点!!
- 求资料:怎样用java实现实时传输视频文件?
- Java2编程起步电子版下载
- #####################很急!!!!!请高手们都来帮帮我!套接字产生的DataOutputStream,在通信时会莫名其妙的被堵死????##########
- 如何写这样一个基类?
- 如果得到java midlet的屏幕大小?
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class ChatClient {
private String name;
private Socket s;
private DataInputStream dis;
private DataOutputStream dos;
private Frame f;
private TextArea ta;
private TextField tf;
private boolean runnable = true;
public static void main(String args[]) {
ChatClient cc = new ChatClient();
cc.createUI();
cc.inputName();
cc.connect();
cc.createThread();
}
public void createUI(){
f = new Frame("Client");
ta = new TextArea();
ta.setEditable(false);
tf = new TextField();
Button send = new Button("Send");
Panel p = new Panel();
p.setLayout(new BorderLayout());
p.add(tf,"Center");
p.add(send,"East");
f.add(ta,"Center");
f.add(p,"South");
MyClientListener listener = new MyClientListener(this);
send.addActionListener(listener);
tf.addActionListener(listener);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
ChatClient.this.shutDown();
}
});
f.setSize(400,400);
f.setLocation(600,0);
f.setVisible(true);
tf.requestFocus();
}
public void inputName(){
String name = javax.swing.JOptionPane.showInputDialog("Input Your Name:");
this.setName(name);
f.setTitle(name);
}
public void connect(){
try {
s = new Socket("127.0.0.1",9999);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
dos.writeUTF(name);
}catch (IOException e) {
e.printStackTrace();
}
}
public void createThread(){
MyClientReader reader = new MyClientReader(this);
reader.start();
}
public void stop(){
runnable = false;
}
public void shutDown(){
try{
dos.writeUTF("bye");
ta.append("Exit in 5 seconds!");
this.stop();
Thread.sleep(5000);
dis.close();
dos.close();
s.close();
}catch(Exception e){
}
System.exit(0);
}
public boolean getRunnable(){
return runnable;
}
public void setName(String name){
this.name = name;
}
public DataInputStream getDataInputStream(){
return dis;
}
public DataOutputStream getDataOutputStream(){
return dos;
}
public TextArea getTextArea(){
return ta;
}
public TextField getTextField(){
return tf;
}
}class MyClientListener implements ActionListener{
private ChatClient client;
public MyClientListener(ChatClient client){
this.client = client;
}
public void actionPerformed(ActionEvent e){
TextField tf = client.getTextField();
String info = tf.getText();
try{
client.getDataOutputStream().writeUTF(info);
}catch (IOException e1) {
e1.printStackTrace();
}
if(info.equals("bye")){
client.shutDown();
}
tf.setText("");
tf.requestFocus();
}
}class MyClientReader extends Thread{
private ChatClient client;
public MyClientReader(ChatClient client){
this.client = client;
}
public void run(){
String info;
DataInputStream dis = client.getDataInputStream();
TextArea ta = client.getTextArea();
try{
while(client.getRunnable()){
info = dis.readUTF();
ta.append(info + "\n");
}
}catch (IOException e) {
}
}
}服务端:
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatServer {
public static void main(String args[]) {
Hashtable<String,DataOutputStream> userList = new Hashtable<String,DataOutputStream>();
String name;
DataInputStream dis;
DataOutputStream dos;
try{
ServerSocket ss = new ServerSocket(9999);
while(true){
Socket s = ss.accept();
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
name = dis.readUTF();
userList.put(name,dos);
new MyServerReader(name,dis,userList).start();
}
}catch(Exception e){
e.printStackTrace();
}
}
}class MyServerReader extends Thread{
private String name;
private DataInputStream dis;
private Hashtable<String,DataOutputStream> userList;
public MyServerReader(String name,DataInputStream dis,Hashtable<String,DataOutputStream> userList ){
this.name = name;
this.dis = dis;
this.userList = userList;
}
public void run(){
String info;
try{
transmitMessage(name + " in!","--Server Info--");
while(true){
info = dis.readUTF();
if(info.equals("bye")){
DataOutputStream dos = (DataOutputStream)(userList.get(name));
Thread.sleep(1000);
dos.close();
dis.close();
userList.remove(name);
transmitMessage(name + " out!","--Server Info--");
break;
}else if(info.length()>0){
transmitMessage(info,name);
}
}
}catch (Exception e) {
}
}
public void transmitMessage(String msg,String name){
Collection doses = userList.values();
DataOutputStream dos;
for(Object o: doses){
dos = (DataOutputStream)o;
try{
dos.writeUTF(name + ":" + msg);
}catch(Exception e){
}
}
}
}
你这个函数如果执行的时候出错那服务端肯定借不到东西的。肯定是这个函数调用的时候出错了
另外,sendStr这个函数参数中有个socket的,你掉的socket传数据,怎么只把缓冲区清出了(java的flush好像是清空并将数据发送,可是你好像这里面没配置socket,发估计会出错)
最后提醒楼主,socket通信数据格式好像挺主要,我记得上次做的时候服务端总是接不到东西就是由于数据最后需要加上"\r\n"
还有socket通信时双向的,就是你往服务端发了一个东西,服务端比返回一个数据证明发送成功或者其他的,然后客户端接收,这算完成一次通信,否则好像是客户端会认为上次发送失败然后提示网络断开