论坛首页 Java企业应用论坛

socket编程

浏览 9933 次
锁定老帖子 主题:socket编程
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (6)
作者 正文
   发表时间:2011-07-19  
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2552)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1297)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at theqq.server$MyThread.run(server.java:157)




代码

package theqq;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.MenuBar;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.*;
import javax.swing.event.MenuDragMouseEvent;
import javax.swing.event.MenuDragMouseListener;



public class server extends JFrame{
Vector<MyThread> allThread = new Vector<MyThread>();
HashMap<String,MyThread> hmh = new HashMap<String,MyThread>();//自己好家对方好  线程
HashMap<String,MyThread> hmhm = new HashMap<String,MyThread>();//qunhao  线程
HashMap<MyThread,String> hmhm1 = new HashMap<MyThread,String>();// 线程  qunhao
//框架设计
public void getKuangJia(){
setTitle("server");
setSize(550, 500);
setVisible(true);
setLocation(200, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//面板设置
JPanel mp = (JPanel) getContentPane();
JPanel view = new JPanel();
JPanel edit = new JPanel();
JPanel person = new JPanel();
JTextArea jt1 = new JTextArea("" ,16,30);
JScrollPane viewItem = new JScrollPane(jt1);
JTextField jt2 = new JTextField("                                                                            ");
JTextArea jt3 = new JTextArea("",4,15);
JScrollPane personItem = new JScrollPane(jt3);
JButton fasong = new JButton("发送");

public void setPanel() throws Exception{
mp.setLayout(new BorderLayout());
mp.add(view,BorderLayout.CENTER);
mp.add(edit,BorderLayout.SOUTH);
mp.add(person,BorderLayout.EAST);
edit.setLayout(new FlowLayout());
view.add(viewItem);
edit.add(jt2);
edit.add(fasong);
person.add(personItem);

}
//网络编程
public void sockes() throws Exception{
ServerSocket ss = new ServerSocket(8888);
Socket s ;

fasong.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < allThread.size(); i++) {
MyMessage mess = new MyMessage(true,jt2.getText(), Calendar.getInstance(), "",  "对方", "9999","23","fuwuqi");
allThread.get(i).sendMessage(mess);
}
}
});

while(true){


s = ss.accept();

MyThread mt = new MyThread(s);
mt.start();
// allThread.add(mt);
//////////////hm.put( mt,s.getRemoteSocketAddress().toString());
jt1.append(s.getRemoteSocketAddress().toString()+"\n");
mt.setHao(s.getRemoteSocketAddress().toString());//这里错了  其实不是hao  是dizhi


// try {

// } catch (Exception e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
}
}


public server() throws Exception {
getKuangJia();
setPanel();
sockes();

}
public static void main(String[] args) {
try {
new server();
} catch (Exception e) {
e.printStackTrace();
}

}

class MyThread extends Thread{
Socket s ;
ObjectInputStream  ois ;
ObjectOutputStream oos ;
public MyThread(Socket s ) {
this.s =s;
}

String hao  = new String();
public String getHao() {
return hao;
}

public void setHao(String hao) {
this.hao = hao;
}

int i =1;
int j =1;
//MyHash_qun  mhq=null;
@Override
public void run() {
try {

ois = new ObjectInputStream(s.getInputStream()) ;

MyMessage mess = null;
String zijiqq = new String();
String duidfang = new String();

while((mess=(MyMessage) ois.readObject())!=null){
if(!mess.getMethodname().equals("data")){
MyDataThread mdt  = new MyDataThread(mess,s);
mdt.start();
}else{
if(!mess.isSi_or_qun()){
if(i<=1){
System.out.println(mess.getZiji_qqno()+"zji_____");
System.out.println(mess.getDuifang_qqno()+"duifang_____");
//
String pingjie  = mess.getDuifang_qqno()+mess.getZiji_qqno();
hmh.put(pingjie, this);
i++;
}
siliao(mess);//私聊
}else{
if(j<=1){
String qunhao = mess.getQun_qqno();
String zijid  = mess.getZiji_qqno();

hmhm.put(qunhao+"##"+zijid, this);
hmhm1.put(this, qunhao+"##"+zijid);
allThread.add(this);
j++;
//mhq = new MyHash_qun(zijid+qunhao, zijid, this);
//hmhm.put(qunhao+"##"+zijid, this);
}
System.out.println("信息"+mess.toString());
qunliao(mess);//群聊
}
}



}


} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}


//私聊
public void siliao(MyMessage mess){

//
String pingjie2 = mess.getZiji_qqno()+mess.getDuifang_qqno();
MyThread mt = hmh.get(pingjie2);
if(mt!=null){
mt.sendMessage(mess);
System.out.println("正常");
}else{
System.out.println("他还没有打开跟你聊天的窗口哦,做个方法提醒他一下吧");//xu yao ti xing da kai
}



}

//群聊
public void qunliao(MyMessage mess) throws IOException{
for (int i = 0; i < allThread.size(); i++) {
System.out.println(allThread.size()+"size");
MyThread mtm = allThread.get(i);
String qun = mess.getQun_qqno();
String qun_zi = hmhm1.get(mtm);
String[] s = qun_zi.split("##");
if(s[0].equals(qun)){
// if(s[1]!=mess.getZiji_qqno()){
mtm.sendMessage(mess);
System.out.println(mess.toString()+"++++=");
// }

}
}
}
public void sendMessage(MyMessage mess){
try {
oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject(mess);
} catch (IOException e) {
e.printStackTrace();
}
}



}



}

class MyDataThread extends Thread {
MyMessage mess;
ObjectOutputStream ooos ;
Socket ss;
public MyDataThread(MyMessage mess,Socket ss) {

this.mess = mess;
this.ss=ss;
}
public void run(){
try {

this.ooos = new ObjectOutputStream(ss.getOutputStream());
if(mess!=null){
MyMessage result  = getResutt(mess);
ooos.writeObject(result);
}
} catch (IOException e) {
e.printStackTrace();
}
}
//访问数据库
public  MyMessage getResutt(MyMessage mess){

MyMessage result=null;
String Methodname = mess.getMethodname();
if(Methodname.trim().equals("user_can_loign")){
System.out.println(mess.getUser().getAcount()+";;;;;;;;;;;;;;;;");
SearchQq ssq = new SearchQq();
User u = (User)mess.getUser();
System.out.println(u.getAcount());
User resultobj = ssq.user_can_loign(u);
result.setUser(resultobj);
}
if(Methodname.trim().equals("add_user")){
SearchQq ssq = new SearchQq();
User u = (User)mess.getUser();
String  acount = ssq.add_user(u);
result.setAcount(acount);
}

return result;
}
}

    
   发表时间:2011-07-21  
oos.writeObject(mess);
后面一定要跟上
oos.flush();方法
0 请登录后投票
   发表时间:2011-07-21  
有点晕,楼主多看API。
0 请登录后投票
   发表时间:2011-07-21  
能不全是代码吗?
0 请登录后投票
   发表时间:2011-07-21  
全是代码,还没格式化,谁看的懂啊,晕
0 请登录后投票
   发表时间:2011-07-22  
额。。。。
面对这一段代码,我表示,很有压力,谁能给我看完的勇气。。。
0 请登录后投票
   发表时间:2011-07-22  
很难看完,当场就晕 了
0 请登录后投票
   发表时间:2011-07-22  
代码没看,但异常看到啦 ^_^,在Tomcat文件夹下按Ctrl+F,然后搜*.ser,把搜出来的都del,应该就OK了!
0 请登录后投票
   发表时间:2011-07-22  
我以前也遇到过,不过你的代码太多了,没法看呀
你去 http://jacky1118sally.iteye.com/admin/blogs/1128332 看看我写这个文章有用么,希望有帮助
0 请登录后投票
   发表时间:2011-07-22  
你这个问题我刚碰到过  是因为你在客户端有多个socket和ois和oos   导致前一个读写和后一个读写混乱  建议你用一个
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics