⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.java

📁 通过网页与服务器通信 客户端是嵌入网页的java小程序,服务器是独立运行的java程序,通过odbc连接access数据库,有聊天记录功能
💻 JAVA
字号:
import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class Server 
{
	public static void main(String args[])
	{
		ServerSocket server=null;
		Server_thread thread;

      Socket you=null;

      while(true) 

       {  try{  server=new ServerSocket(8001);

             }

          catch(IOException e1) 

             {  System.out.println("正在监听"); //ServerSocket对象不能重复创建

             } 

          try{  System.out.println(" 等待客户呼叫");

                you=server.accept();

                System.out.println("客户的地址:"+you.getInetAddress());

             }

         catch (IOException e)

             {  System.out.println("正在等待客户");

             }

         if(you!=null) 

               {  new Server_thread(you).start(); //为每个客户启动一个专门的线程  

             }

       }

   }
   
}

class Server_thread extends Thread implements ActionListener
{
	
	JFrame frame;
	JTextArea liaotian,xiaoxi;
	JButton fasong,guanbi;
	Socket socket;
	JLabel xinxi;
	DataOutputStream out=null;
	DataInputStream  in=null;
	String s=null,qhao=null,nichen=null,ss=null;
	Connection con=null;
	Statement sql;
	ResultSet rs;
	boolean jixu=false;
   Server_thread(Socket t)
	{
		socket=t;

      try {  out=new DataOutputStream(socket.getOutputStream());

             in=new DataInputStream(socket.getInputStream());

          }

      catch (IOException e)

          {}
          
     try{
     	s=in.readUTF();
     	}catch(IOException e)
     	{
     	}
		try {  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  

          }

     catch(ClassNotFoundException e)

          {  System.out.println(""+e);

          }

      try {  con=DriverManager.getConnection("jdbc:odbc:qqhao","alan","");

             sql=con.createStatement();

             rs=sql.executeQuery("SELECT * FROM qq");

             while(rs.next())

             { 
               qhao=rs.getString("qhao");
               
               if(qhao.equals(s))
               {
               	jixu=true;
               	 nichen=rs.getString("nichen"); 
               	 break;
               }
			
              
             }

           }

      catch(SQLException e) 

           {  System.out.println(e.toString());

           }
   }
   public void run()
   {
   	if(!jixu)
   	{
   		try{
   		
   		out.writeUTF("无");
   		con.close();
   		in.close();
   		out.close();
   		socket.close();
   		return;
   		}catch(Exception e)
     	{
     	}
   	}
   	
   	
   	
   	frame=new JFrame("与"+nichen+"交谈中");
   	frame.addNotify();
   	frame.setSize(500,500);
   	JPanel p1,p2;
   	p1=new JPanel(new BorderLayout());
   	p2=new JPanel(new BorderLayout());
	liaotian=new JTextArea(15,15);
	xiaoxi=new JTextArea(5,52);
	liaotian.setLineWrap(true);
	liaotian.setEditable(false);
	xiaoxi.setLineWrap(true);	
	fasong=new JButton("发送");
	guanbi=new JButton("关闭");
	xinxi=new JLabel(s+"  "+nichen);
	p2.add(guanbi,BorderLayout.WEST);
	p2.add(xinxi,BorderLayout.CENTER);
	p2.add(fasong,BorderLayout.EAST);
	p1.add(new JScrollPane(xiaoxi),BorderLayout.CENTER);
	p1.add(p2,BorderLayout.SOUTH);
	frame.getContentPane().add(new JScrollPane(liaotian),BorderLayout.CENTER);
	frame.getContentPane().add(p1,BorderLayout.SOUTH);
	frame.setVisible(true);
	fasong.addActionListener(this);
	guanbi.addActionListener(this);
	frame.validate();
	frame.addWindowListener(new WindowAdapter()
	{
		public void windowClosing(WindowEvent e4)
		{
		try{
				out.writeUTF("关闭了...");
			con.close();
   		in.close();
   		out.close();
   		socket.close();
   		return;}catch(Exception e)
     	{
     	}
		}
	});
	try
	{
	
	out.writeUTF(s);
	out.writeUTF(nichen);
	out.writeUTF("可以与尐強交谈了"+'\n');
	}catch(IOException e)
     	{
     	}
	liaotian.append("已建立联系"+'\n');
		while(true){
			try{
				ss=in.readUTF();
				liaotian.append(ss);
			}catch(IOException e4){
				xinxi.setText("断开联系..");
				break;
			}
		}
	
	
   }
   
   public void actionPerformed(ActionEvent eee)
   {
   	if(eee.getSource()==fasong)
   	{
   		String x=xiaoxi.getText().trim();
		if(x!=null){
		try{
		out.writeUTF("尐強"+":"+x+"\n");
		}catch(IOException e1){
		}
		liaotian.append("尐強"+":"+x+"\n");
		xiaoxi.setText("");
   	}
   }
   else 
   {
   	;
   }
   
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -