| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 872 人关注过本帖
标题:基于socket套接字的网络程序设计(求助!!!!!!!!!!)
只看楼主 加入收藏
qq81928429
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-5-28
收藏
 问题点数:0 回复次数:2 
基于socket套接字的网络程序设计(求助!!!!!!!!!!)
客户端发的信令 服务器为什么接收不到啊(接收时出现异常)!!!!
大家抽点时间帮我看看,小弟感感激不尽!!!!!
服务器端
package JQQpackage;
import *;
import *;
import java.util.*;

public class JQQserver extends Thread
{
    Socket socket;
    PrintStream print;
    BufferedReader in ;
    ServerSocket server;
 
    //此方法用于向文件中读取注册信息
   /*
    public Vector readShuju()throws Exception  
     {
             
               File jqqFile = new File("jqqFile.txt");             //实例一个文件对像
               FileInputStream FileIn = new FileInputStream(jqqFile); //创建一个文件输入流
               ObjectInputStream ObjectIn = new ObjectInputStream(FileIn);//创建一个对像输出流
               Vector v = (Vector)(ObjectIn.readObject());              //反序列化
               ObjectIn.close();
               return v;
               
     }
    */
    public void send(String message)//发送消息
    {
      try{
          print.println(message);
      }
      catch(Exception er)
      {
          System.out.println(er.getMessage());
      }
        
    }
    
    
    public String recerve() //接收消息
    {  
        String str="" ;
        try{
            str = in.readLine();
          }
        catch(Exception er)
        {
            System.out.println(er.getMessage());
        }
        System.out.println(str);
        return str;
        
    }    
    
   public void run() //用于处理客户请求
   {
       
 
    //    Vector v = new Vector(1,1);
     //  try
         //  {
          //  v = readShuju(); //读取文件中的向量
         //  }
       //  catch(Exception er)
       //   {
         //    System.out.println(er.getMessage());
        //  }
          
   
       
       
       String str=recerve();    //客户端发的信令读不出来啊!!!!!!!!!!!!!!!!!!!!!!!!!!!!
           
     //while((str = recerve())!=null)
         System.out.println(str);
         
     /*
    while(!((str=recerve()).equals("Bye")))
    {
        if(str.equals("Login"))             //处理登陆信令
        {

          send("收到 Login信令!!!!!");
          String Account = recerve();
          String Password = recerve();
          int i=0;
          for(; i<v.size(); i++)
          {
              if(((massageFZ)v.get(i)).HMstr1.equals(Account) && ((massageFZ)v.get(i)).password1.equals(Password))
               {
                  
                 break;
               }      
          }  
              if(i>=v.size())
               send("验证不通过!!!!");
               else
                send("验证通过");     
        }    
    }
   
        closeStream();
       
       */
           
   }
   
   
   public void closeStream()
   {
       try{
       
       in.close();
       print.close();
       }
       catch(Exception er)
       {
           
       }
   }
   
   public void     CreatServer()throws Exception
   {   
           server = new ServerSocket(9292);
           JQQserver serverThread = new JQQserver();
        while(true)
        {
            socket = server.accept();
            System.out.println("服务器已启动......");
              in = new BufferedReader(new InputStreamReader(socket.getInputStream())); //创建输入流
            
              print = new PrintStream(socket.getOutputStream()); //创建输出流
              
            serverThread.start();//当接听到客户端请求时启动线程处理客户请求.
            
        }
   }    
    public static void main(String[] args)
    {
        
        JQQserver server = new JQQserver();
        try
        {
            server.CreatServer();
        }
        catch(Exception er)
        {
        System.out.println(er.getMessage());    
        }
    }

    
}

客户端
package JQQpackage;

import java.awt.*;
import javax.swing.*;
import java.util.*;
import *;
import java.awt.event.*;
import *;

public class Daberkaction implements ActionListener,Serializable,MouseListener //登陆界面
  {
      
       Socket socket;
     PrintStream print;
     BufferedReader in ;
    
    JFrame     jframe=new JFrame("JQQ登录界面" );
    JPanel     pane=new JPanel(new FlowLayout(FlowLayout.LEFT,12,14));//设置布局管理器为顺序布局并设置组件的行列间隔
    JLabel     Account=new JLabel("JQQ账号");
    JTextField AccountText=new JTextField(11);
    
    JLabel password=new JLabel("JQQ密码");
    JPasswordField passwordText=new JPasswordField(11);
    
    JButton Daberkaction=new JButton("登录");
    JButton Login=new JButton("注册");
    JButton Eixt=new JButton("退出");

        
    Daberkaction()
    {
        pane.add(Account);      
        pane.add(AccountText);
        AccountText.addMouseListener(this);//为账号框注册鼠标事件
        JLabel A=new JLabel("                 ");
        pane.add(A);
        pane.add(password);
        pane.add(passwordText);
        passwordText.addMouseListener(this);
        
        JLabel B=new JLabel("               ");
        pane.add(B);
        pane.add(Daberkaction);
        JLabel C=new JLabel(" ");
        pane.add(Login);
        JLabel D=new JLabel(" ");
        pane.add(Eixt);
        
        Daberkaction.addActionListener(this);
        Daberkaction.setActionCommand("登录");
        Login.addActionListener(this);
        Login.setActionCommand("注册");
        Eixt.addActionListener(this);
        Eixt.setActionCommand("退出");
        jframe.add(pane);
        jframe.setSize(280,160);
        pane.setBackground(Color.getHSBColor(150,180,180)); //设置面板背景色
        jframe.setResizable(false);                  //设置面板为不可改变大小
        jframe.setVisible(true);                       //设置面板为可见
        jframe.setLocation(200,200);              //设置窗口在屏幕中显示的位置
             
       }
       public void mousePressed(MouseEvent e)
       {
           
       }
       public void mouseReleased(MouseEvent e)
       {
           
       }
       public void mouseEntered(MouseEvent e)
       {
           
       }
       public void mouseExited(MouseEvent e)
       {
           
       }
       public void mouseClicked(MouseEvent e)    //验证账号框中输入字符是否合法
       {
           if((JTextField)e.getSource()==passwordText)
           {
               if(AccountText.getText().equals(""))
               {
                   JOptionPane.showMessageDialog(jframe,"账号不能为空!!");
               }
               if(AccountText.getText().length()>=7)
               {
                   JOptionPane.showMessageDialog(jframe,"账号长度不能超过7位!!!");
               }
               for(int i=0;i<AccountText.getText().length();i++)
               {
                   if(AccountText.getText().charAt(i)>='0'&&AccountText.getText().charAt(i)<='9')
                     ;
                     else
                     {
                         JOptionPane.showMessageDialog(jframe,"账号框中含有非法字符");
                     }
                       
               }
           }
       }
       
       public void send(String message)//发送消息
       {
         try
           {
        print.println(message);
           }
         catch(Exception er)
           {
               
           }
           
       }
       
       
       public String reserve()//接收消息
       {    
           String str="";
           try{
            str = in.readLine();
        System.out.println(str);
           }
           catch(Exception er)
           {    
           }
       return str;
       }
           
           
       
           
       // 行为事件处理
    public void actionPerformed(ActionEvent e)
     {
           String getAccount  = AccountText.getText();//得到登陆界面账号框值
          String getPassword = passwordText.getText();//得到登陆界面密码框值         
          
                
        if(e.getActionCommand().equals("登录"))
        {
            try{
            
            socket = new Socket("192.168.0.178",9292);  //创建与服务器连接的套接字
            System.out.println("已连接上 ..........");
            print = new PrintStream(socket.getOutputStream()); //创建输出流
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));//创建输入流
           
            }
            catch(Exception er)
            {
                
            }
            
            
          send("Login");   //向服务器发送 登陆的信令
          System.out.println(reserve());
          send(getAccount);
          send(getPassword);
        
          if(reserve().equals("验证通过"))
          {
              send("Bye");
              System.out.println("登陆成功");
          }
         else
           {
             JOptionPane.showMessageDialog(jframe,"登陆失败密码或帐号不存在!!");    
                 send("Bye");
          }
          
          
       }
        
        
        if(e.getActionCommand().equals("注册"))
        {
        
        //    LoginInterfase  login1 = new LoginInterfase(); //调出注册界面
            
        }
        if(e.getActionCommand().equals("退出"))
        {
            jframe.setVisible(false);
        }
    }
    
    public static void main(String[] args)
    {
        Daberkaction daber = new Daberkaction();
    }
    
  }                                                                      //登陆界面结束
搜索更多相关主题的帖子: 网络 套接字 程序设计 socket 
2008-06-04 16:20
qq81928429
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-5-28
收藏
得分:0 
大家用机子运行一下。。。
2008-06-04 16:36
huhu2hao
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-6-14
收藏
得分:0 
J-QQ...晕..你是科技学院的吧...
2008-06-14 11:38
快速回复:基于socket套接字的网络程序设计(求助!!!!!!!!!!)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.060157 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved