找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 3118|回复: 1

[VB6+Java]点对点聊天程序

[复制链接]

17

主题

28

回帖

590

积分

用户组: 大·技术宅

UID
140
精华
5
威望
30 点
宅币
440 个
贡献
26 次
宅之契约
0 份
在线时间
54 小时
注册时间
2014-3-22
发表于 2014-10-2 13:20:50 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×
本帖最后由 mzflz 于 2014-10-2 13:24 编辑

客户端使用vb6写的
服务器端是用Java写的
VB6需添加Winsock activeX控件
程序比较一般 ,没有什么特别的

==客户端==
  1. VERSION 5.00
  2. Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
  3. Begin VB.Form Form1
  4.    BorderStyle     =   3  'Fixed Dialog
  5.    Caption         =   "Chat"
  6.    ClientHeight    =   5550
  7.    ClientLeft      =   45
  8.    ClientTop       =   390
  9.    ClientWidth     =   8355
  10.    LinkTopic       =   "Form1"
  11.    MaxButton       =   0   'False
  12.    MinButton       =   0   'False
  13.    ScaleHeight     =   5550
  14.    ScaleWidth      =   8355
  15.    ShowInTaskbar   =   0   'False
  16.    StartUpPosition =   3  '窗口缺省
  17.    Begin MSWinsockLib.Winsock Winsock1
  18.       Left            =   3000
  19.       Top             =   4920
  20.       _ExtentX        =   741
  21.       _ExtentY        =   741
  22.       _Version        =   393216
  23.    End
  24.    Begin VB.Frame Frame2
  25.       Caption         =   "聊天"
  26.       Height          =   5175
  27.       Left            =   3360
  28.       TabIndex        =   6
  29.       Top             =   240
  30.       Width           =   4815
  31.       Begin VB.CommandButton Command2
  32.          Caption         =   "发送"
  33.          Enabled         =   0   'False
  34.          Height          =   375
  35.          Left            =   4080
  36.          TabIndex        =   9
  37.          Top             =   4560
  38.          Width           =   615
  39.       End
  40.       Begin VB.TextBox Text4
  41.          Enabled         =   0   'False
  42.          ForeColor       =   &H00C0C0C0&
  43.          Height          =   375
  44.          Left            =   120
  45.          TabIndex        =   8
  46.          Text            =   "输入你想要发送的的信息"
  47.          Top             =   4560
  48.          Width           =   3855
  49.       End
  50.       Begin VB.TextBox Text3
  51.          Enabled         =   0   'False
  52.          ForeColor       =   &H00000000&
  53.          Height          =   4095
  54.          Left            =   120
  55.          MultiLine       =   -1  'True
  56.          ScrollBars      =   2  'Vertical
  57.          TabIndex        =   7
  58.          Top             =   360
  59.          Width           =   4575
  60.       End
  61.    End
  62.    Begin VB.Frame Frame1
  63.       Caption         =   "登录"
  64.       Height          =   3135
  65.       Left            =   120
  66.       TabIndex        =   0
  67.       Top             =   240
  68.       Width           =   3135
  69.       Begin VB.CommandButton Command3
  70.          Caption         =   "连接"
  71.          Height          =   615
  72.          Left            =   120
  73.          TabIndex        =   14
  74.          Top             =   2400
  75.          Width           =   1215
  76.       End
  77.       Begin VB.TextBox Text6
  78.          ForeColor       =   &H00C0C0C0&
  79.          Height          =   390
  80.          Left            =   840
  81.          TabIndex        =   11
  82.          Text            =   "1234"
  83.          Top             =   1920
  84.          Width           =   2055
  85.       End
  86.       Begin VB.TextBox Text5
  87.          ForeColor       =   &H00C0C0C0&
  88.          Height          =   375
  89.          Left            =   840
  90.          TabIndex        =   10
  91.          Text            =   "127.0.0.1"
  92.          Top             =   1440
  93.          Width           =   2055
  94.       End
  95.       Begin VB.CommandButton Command1
  96.          Caption         =   "登录"
  97.          Height          =   615
  98.          Left            =   1440
  99.          TabIndex        =   5
  100.          Top             =   2400
  101.          Width           =   1575
  102.       End
  103.       Begin VB.TextBox Text2
  104.          ForeColor       =   &H00C0C0C0&
  105.          Height          =   375
  106.          Left            =   840
  107.          TabIndex        =   2
  108.          Text            =   "mzflz"
  109.          Top             =   960
  110.          Width           =   2055
  111.       End
  112.       Begin VB.TextBox Text1
  113.          ForeColor       =   &H00C0C0C0&
  114.          Height          =   375
  115.          Left            =   840
  116.          TabIndex        =   1
  117.          Text            =   "Mazhenfeng"
  118.          Top             =   480
  119.          Width           =   2055
  120.       End
  121.       Begin VB.Label Label4
  122.          Caption         =   "端口"
  123.          Height          =   255
  124.          Left            =   120
  125.          TabIndex        =   13
  126.          Top             =   1920
  127.          Width           =   495
  128.       End
  129.       Begin VB.Label Label3
  130.          Caption         =   "IP地址"
  131.          Height          =   375
  132.          Left            =   120
  133.          TabIndex        =   12
  134.          Top             =   1440
  135.          Width           =   615
  136.       End
  137.       Begin VB.Label Label2
  138.          Caption         =   "密码"
  139.          Height          =   375
  140.          Left            =   120
  141.          TabIndex        =   4
  142.          Top             =   960
  143.          Width           =   615
  144.       End
  145.       Begin VB.Label Label1
  146.          Caption         =   "账号"
  147.          Height          =   375
  148.          Left            =   120
  149.          TabIndex        =   3
  150.          Top             =   480
  151.          Width           =   615
  152.       End
  153.    End
  154.    Begin VB.Image Image1
  155.       Height          =   1740
  156.       Left            =   240
  157.       Picture         =   "Form1.frx":0000
  158.       Top             =   3480
  159.       Width           =   2685
  160.    End
  161. End
  162. Attribute VB_Name = "Form1"
  163. Attribute VB_GlobalNameSpace = False
  164. Attribute VB_Creatable = False
  165. Attribute VB_PredeclaredId = True
  166. Attribute VB_Exposed = False
  167. Private Declare Function Sleep Lib "kernel32" (time As Integer) As Integer



  168. 'Dim isRight As Integer
  169. Dim a As Integer

  170. Private Sub Command1_Click()


  171. If Winsock1.State = sckClosed Then
  172.     MsgBox "Not Connected!", vbOKOnly, "Error"
  173. Else
  174.     'Winsock1.SendData "Hello"
  175.     Winsock1.SendData (Text1.Text)
  176.     Winsock1.SendData vbCrLf
  177.     Winsock1.SendData (Text2.Text)
  178.     Winsock1.SendData vbCrLf

  179.    ' Else
  180.      '   Winsock1.Close

  181. End If

  182. End Sub

  183. Private Sub Command2_Click()
  184. If Text4.Text = "" Or Text4.Text = "输入你想要发送的信息" Then
  185. MsgBox "发送内容不能为空", vbOKOnly, "错误"
  186. Else
  187.     Dim str As String
  188.      str = Text4.Text + vbCrLf
  189.     Winsock1.SendData str
  190.     Text3.Text = Text3.Text + Format(time, "Long Time") + vbCrLf + str
  191.     Text4.ForeColor = &HC0C0C0
  192.     Text4.Text = "输入你想要发送的信息"
  193. End If
  194. End Sub

  195. Private Sub Command3_Click()
  196. Winsock1.Connect Text5.Text, CInt(Text6.Text)  '开始连接
  197. End Sub

  198. Private Sub Form_Load()
  199. a = 0
  200. isRight = 0
  201. 'Winsock1.Connect Text5.Text, CInt(Text6.Text)  '开始连接
  202. End Sub

  203. Private Sub Text1_GotFocus()
  204. 'Text1.Text = ""
  205. 'Text1.ForeColor = vbBlack
  206. End Sub

  207. Private Sub Text1_LostFocus()
  208. If Text1.Text = "" Then
  209. Text1.ForeColor = &HC0C0C0
  210. Text1.Text = "请输入用户名"
  211. End If
  212. End Sub

  213. Private Sub Text2_GotFocus()
  214. Text2.Text = ""
  215. Text2.ForeColor = vbBlack
  216. End Sub

  217. Private Sub Text2_LostFocus()
  218. If Text2.Text = "" Then
  219. Text2.ForeColor = &HC0C0C0
  220. Text2.Text = "请输入密码"
  221. End If
  222. End Sub

  223. Private Sub Text4_GotFocus()
  224. Text4.ForeColor = vbBlack
  225. Text4.Text = ""
  226. End Sub

  227. Private Sub Text4_LostFocus()
  228. If Text4.Text = "" Then
  229. Text4.ForeColor = &HC0C0C0
  230. Text4.Text = "输入你想要发送的信息"
  231. End If
  232. End Sub

  233. Private Sub Text5_GotFocus()
  234. Text5.Text = ""
  235. Text5.ForeColor = vbBlack
  236. End Sub

  237. Private Sub Text5_LostFocus()
  238. If Text5.Text = "" Then
  239. Text5.ForeColor = &HC0C0C0
  240. Text5.Text = "请输入对方IP"
  241. End If

  242. End Sub

  243. Private Sub Text6_GotFocus()
  244. Text6.Text = ""
  245. Text6.ForeColor = vbBlack
  246. End Sub

  247. Private Sub Text6_LostFocus()
  248. If Text6.Text = "" Then
  249. Text6.ForeColor = &HC0C0C0
  250. Text6.Text = "请输入端口号"
  251. End If
  252. End Sub

  253. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
  254. a = a + 1
  255. Dim isRight As String
  256. If a = 1 Then
  257. Winsock1.GetData isRight
  258. If (isRight) <> "0" Then
  259.     Text4.Enabled = True
  260.     Text3.Enabled = True
  261.     Command2.Enabled = True
  262.     Text1.Enabled = False
  263.     Text2.Enabled = False
  264.     Text5.Enabled = False
  265.     Text6.Enabled = False
  266.     Command3.Enabled = False
  267.     Command1.Enabled = False
  268.     End If
  269.     Else
  270.     Dim str As String
  271.     Winsock1.GetData str
  272.     Text3.Text = Text3.Text + Format(time, "Long Time") + vbCrLf + str + vbCrLf
  273.     End If
  274. End Sub

复制代码


==服务器端==
  1. package main;
  2. import java.net.*;
  3. import java.awt.BorderLayout;
  4. import java.awt.Button;
  5. import java.awt.Frame;
  6. import java.awt.Panel;
  7. import java.awt.TextArea;
  8. import java.awt.TextField;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11. import java.awt.event.WindowAdapter;
  12. import java.awt.event.WindowEvent;
  13. import java.io.*;
  14. import java.util.Date;
  15. import java.util.Locale;
  16. import java.util.Scanner;

  17. import javax.swing.JOptionPane;
  18. public class Main {
  19.         private static Exception Choose;

  20.         public static void main(String args[]){
  21.                
  22.                 BufferedReader brr = null;
  23.                 OutputStream os = null;
  24.                 ServerSocket ss = null;
  25.                 Socket s = null;
  26.                 Scanner scan = new Scanner(System.in);
  27.                 try
  28.                 {
  29.                         ss = new ServerSocket(1234);
  30.                         s = ss.accept();
  31.                         brr = new BufferedReader(new InputStreamReader(s.getInputStream()));
  32.                         os = (s.getOutputStream());
  33.                         String str1 = brr.readLine();
  34.                         String str2 = brr.readLine();
  35.                         System.out.println(str1+"\n"+str2);
  36.                         
  37.                         if(str1.equals("Mazhenfeng")&&str2.equals("mzflz"))
  38.                         {
  39.                                 System.out.println("OK");
  40.                                 os.write(new String("1").getBytes());
  41.                         }
  42.                         else
  43.                                 os.write(new String("0").getBytes());
  44.                         //C:
  45.                         new MyFrame("Chat",brr,os);
  46.                         System.out.println("------------------------------------");
  47.                         System.out.print("1:接收                                                      2,发送\n");
  48.                         int a = scan.nextInt();
  49.                         if(a == 1)
  50.                         {        while(true)
  51.                                 {
  52.                                         System.out.println(brr.readLine());
  53.                                 }
  54.                         
  55.                         }
  56.                         else
  57.                         {
  58.                                 while(true)
  59.                                 {
  60.                                         String str = scan.nextLine();
  61.                                         //if (str.equals("rechoose"))
  62.                                                 //break C;
  63.                                         os.write(str.getBytes());
  64.                                 }
  65.                         }
  66.                         
  67.                 }
  68.                 catch(Exception e)
  69.                 {
  70.                         System.out.println(e);
  71.                 }
  72.         }
  73. }
  74. class MyFrame extends Frame
  75. {
  76.         BufferedReader bff = null
  77.                         ;
  78.         OutputStream os = null;
  79.         TextField p_tf = null;
  80.         Button b = null;
  81.         Panel p =null;
  82.         TextArea  ta =null;
  83.         Thread t = null;
  84.         public MyFrame(String str,BufferedReader bff,OutputStream os)
  85.         {
  86.                 super(str);
  87.                
  88.                 this.bff = bff;
  89.                 this.os = os;
  90.                 setLocation(100,100);
  91.                 //setSize(900,500);
  92.                 ta = new TextArea(50,50);
  93.                  p = new Panel();
  94.                  b = new Button("发送");
  95.                 p_tf = new TextField(50);
  96.                 add(ta,BorderLayout.NORTH);
  97.                 add(p,BorderLayout.SOUTH);
  98.                 p.add(p_tf);
  99.                 p.add(b);
  100.                 b.addActionListener(new ActionListener()
  101.                 {

  102.                         public void actionPerformed(ActionEvent arg0) {
  103.                                 // TODO Auto-generated method stub
  104.                                 try {
  105.                                         b_event();
  106.                                 } catch (IOException e) {
  107.                                         // TODO Auto-generated catch block
  108.                                         e.printStackTrace();
  109.                                 }
  110.                                 
  111.                         }
  112.                         
  113.                         
  114.                 });
  115.                 addWindowListener(new WindowAdapter()
  116.                 {

  117.                         public void windowClosing(WindowEvent arg0) {
  118.                                 t.interrupt();
  119.                                 System.exit(0);
  120.                                 
  121.                                 // TODO Auto-generated method stub
  122.                                 super.windowClosing(arg0);
  123.                         }
  124.                         
  125.                 });
  126.                 setVisible(true);
  127.                 pack();
  128.                 t = new Thread(new MyRunnable(bff,ta));
  129.                 t.start();
  130.                
  131.         }
  132.         void b_event() throws IOException
  133.         {
  134.                
  135.                 if(!(p_tf.getText().isEmpty()))
  136.                 {
  137.                         ta.append("Me"+new Date().toString()+"\n"+p_tf.getText()+"\n");
  138.                         os.write((p_tf.getText()+"\n").getBytes());
  139.                 }
  140.                 else
  141.                 {
  142.                         JOptionPane.showMessageDialog(null,"不能为空");
  143.                 }
  144.                 p_tf.setText("");
  145.                
  146.         }

  147. }
  148. class MyRunnable implements Runnable{
  149.         BufferedReader brr = null;
  150.         TextArea ta = null;
  151.         String str =null;
  152.         public MyRunnable(BufferedReader brr,TextArea ta)
  153.         {
  154.                 this.brr = brr;
  155.                 this.ta = ta;
  156.         }
  157.         public void run() {
  158.                 while(true)
  159.                 {
  160.                         try {
  161.                                 str = brr.readLine();
  162.                                 ta.append("Mazhenfeng    "+new Date().toString()+"\n"+str+"\n");
  163.                         } catch (IOException e) {
  164.                                 // TODO Auto-generated catch block
  165.                                 e.printStackTrace();
  166.                         }
  167.                         
  168.                 }
  169.         }
  170. }
复制代码

未连接前

客户端
QQ截图20141002131127.png

服务器端
^-^ (无 界面)

连接后


客户端
QQ截图20141002131423.png

服务器端

QQ截图20141002131429.png





Chat.zip

11.87 KB, 下载次数: 18

回复

使用道具 举报

KxIX 该用户已被删除
发表于 2014-10-13 22:05:59 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-4-20 16:15 , Processed in 0.042062 second(s), 38 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表