- UID
- 140
- 精华
- 积分
- 606
- 威望
- 点
- 宅币
- 个
- 贡献
- 次
- 宅之契约
- 份
- 最后登录
- 1970-1-1
- 在线时间
- 小时
|
本帖最后由 mzflz 于 2014-10-2 13:24 编辑
客户端使用vb6写的
服务器端是用Java写的
VB6需添加Winsock activeX控件
程序比较一般 ,没有什么特别的
==客户端==
==服务器端==
- package main;
- import java.net.*;
- import java.awt.BorderLayout;
- import java.awt.Button;
- import java.awt.Frame;
- import java.awt.Panel;
- import java.awt.TextArea;
- import java.awt.TextField;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.io.*;
- import java.util.Date;
- import java.util.Locale;
- import java.util.Scanner;
- import javax.swing.JOptionPane;
- public class Main {
- private static Exception Choose;
- public static void main(String args[]){
-
- BufferedReader brr = null;
- OutputStream os = null;
- ServerSocket ss = null;
- Socket s = null;
- Scanner scan = new Scanner(System.in);
- try
- {
- ss = new ServerSocket(1234);
- s = ss.accept();
- brr = new BufferedReader(new InputStreamReader(s.getInputStream()));
- os = (s.getOutputStream());
- String str1 = brr.readLine();
- String str2 = brr.readLine();
- System.out.println(str1+"\n"+str2);
-
- if(str1.equals("Mazhenfeng")&&str2.equals("mzflz"))
- {
- System.out.println("OK");
- os.write(new String("1").getBytes());
- }
- else
- os.write(new String("0").getBytes());
- //C:
- new MyFrame("Chat",brr,os);
- System.out.println("------------------------------------");
- System.out.print("1:接收 2,发送\n");
- int a = scan.nextInt();
- if(a == 1)
- { while(true)
- {
- System.out.println(brr.readLine());
- }
-
- }
- else
- {
- while(true)
- {
- String str = scan.nextLine();
- //if (str.equals("rechoose"))
- //break C;
- os.write(str.getBytes());
- }
- }
-
- }
- catch(Exception e)
- {
- System.out.println(e);
- }
- }
- }
- class MyFrame extends Frame
- {
- BufferedReader bff = null
- ;
- OutputStream os = null;
- TextField p_tf = null;
- Button b = null;
- Panel p =null;
- TextArea ta =null;
- Thread t = null;
- public MyFrame(String str,BufferedReader bff,OutputStream os)
- {
- super(str);
-
- this.bff = bff;
- this.os = os;
- setLocation(100,100);
- //setSize(900,500);
- ta = new TextArea(50,50);
- p = new Panel();
- b = new Button("发送");
- p_tf = new TextField(50);
- add(ta,BorderLayout.NORTH);
- add(p,BorderLayout.SOUTH);
- p.add(p_tf);
- p.add(b);
- b.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent arg0) {
- // TODO Auto-generated method stub
- try {
- b_event();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- }
-
-
- });
- addWindowListener(new WindowAdapter()
- {
- public void windowClosing(WindowEvent arg0) {
- t.interrupt();
- System.exit(0);
-
- // TODO Auto-generated method stub
- super.windowClosing(arg0);
- }
-
- });
- setVisible(true);
- pack();
- t = new Thread(new MyRunnable(bff,ta));
- t.start();
-
- }
- void b_event() throws IOException
- {
-
- if(!(p_tf.getText().isEmpty()))
- {
- ta.append("Me"+new Date().toString()+"\n"+p_tf.getText()+"\n");
- os.write((p_tf.getText()+"\n").getBytes());
- }
- else
- {
- JOptionPane.showMessageDialog(null,"不能为空");
- }
- p_tf.setText("");
-
- }
- }
- class MyRunnable implements Runnable{
- BufferedReader brr = null;
- TextArea ta = null;
- String str =null;
- public MyRunnable(BufferedReader brr,TextArea ta)
- {
- this.brr = brr;
- this.ta = ta;
- }
- public void run() {
- while(true)
- {
- try {
- str = brr.readLine();
- ta.append("Mazhenfeng "+new Date().toString()+"\n"+str+"\n");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- }
- }
- }
复制代码
未连接前
客户端
服务器端
^-^ (无 界面)
连接后
客户端
服务器端
|
|