[VB6+Java]点对点聊天程序
本帖最后由 mzflz 于 2014-10-2 13:24 编辑客户端使用vb6写的
服务器端是用Java写的
VB6需添加Winsock activeX控件
程序比较一般 ,没有什么特别的
==客户端==
VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
Begin VB.Form Form1
BorderStyle = 3'Fixed Dialog
Caption = "Chat"
ClientHeight = 5550
ClientLeft = 45
ClientTop = 390
ClientWidth = 8355
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 5550
ScaleWidth = 8355
ShowInTaskbar = 0 'False
StartUpPosition = 3'窗口缺省
Begin MSWinsockLib.Winsock Winsock1
Left = 3000
Top = 4920
_ExtentX = 741
_ExtentY = 741
_Version = 393216
End
Begin VB.Frame Frame2
Caption = "聊天"
Height = 5175
Left = 3360
TabIndex = 6
Top = 240
Width = 4815
Begin VB.CommandButton Command2
Caption = "发送"
Enabled = 0 'False
Height = 375
Left = 4080
TabIndex = 9
Top = 4560
Width = 615
End
Begin VB.TextBox Text4
Enabled = 0 'False
ForeColor = &H00C0C0C0&
Height = 375
Left = 120
TabIndex = 8
Text = "输入你想要发送的的信息"
Top = 4560
Width = 3855
End
Begin VB.TextBox Text3
Enabled = 0 'False
ForeColor = &H00000000&
Height = 4095
Left = 120
MultiLine = -1'True
ScrollBars = 2'Vertical
TabIndex = 7
Top = 360
Width = 4575
End
End
Begin VB.Frame Frame1
Caption = "登录"
Height = 3135
Left = 120
TabIndex = 0
Top = 240
Width = 3135
Begin VB.CommandButton Command3
Caption = "连接"
Height = 615
Left = 120
TabIndex = 14
Top = 2400
Width = 1215
End
Begin VB.TextBox Text6
ForeColor = &H00C0C0C0&
Height = 390
Left = 840
TabIndex = 11
Text = "1234"
Top = 1920
Width = 2055
End
Begin VB.TextBox Text5
ForeColor = &H00C0C0C0&
Height = 375
Left = 840
TabIndex = 10
Text = "127.0.0.1"
Top = 1440
Width = 2055
End
Begin VB.CommandButton Command1
Caption = "登录"
Height = 615
Left = 1440
TabIndex = 5
Top = 2400
Width = 1575
End
Begin VB.TextBox Text2
ForeColor = &H00C0C0C0&
Height = 375
Left = 840
TabIndex = 2
Text = "mzflz"
Top = 960
Width = 2055
End
Begin VB.TextBox Text1
ForeColor = &H00C0C0C0&
Height = 375
Left = 840
TabIndex = 1
Text = "Mazhenfeng"
Top = 480
Width = 2055
End
Begin VB.Label Label4
Caption = "端口"
Height = 255
Left = 120
TabIndex = 13
Top = 1920
Width = 495
End
Begin VB.Label Label3
Caption = "IP地址"
Height = 375
Left = 120
TabIndex = 12
Top = 1440
Width = 615
End
Begin VB.Label Label2
Caption = "密码"
Height = 375
Left = 120
TabIndex = 4
Top = 960
Width = 615
End
Begin VB.Label Label1
Caption = "账号"
Height = 375
Left = 120
TabIndex = 3
Top = 480
Width = 615
End
End
Begin VB.Image Image1
Height = 1740
Left = 240
Picture = "Form1.frx":0000
Top = 3480
Width = 2685
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Declare Function Sleep Lib "kernel32" (time As Integer) As Integer
'Dim isRight As Integer
Dim a As Integer
Private Sub Command1_Click()
If Winsock1.State = sckClosed Then
MsgBox "Not Connected!", vbOKOnly, "Error"
Else
'Winsock1.SendData "Hello"
Winsock1.SendData (Text1.Text)
Winsock1.SendData vbCrLf
Winsock1.SendData (Text2.Text)
Winsock1.SendData vbCrLf
' Else
' Winsock1.Close
End If
End Sub
Private Sub Command2_Click()
If Text4.Text = "" Or Text4.Text = "输入你想要发送的信息" Then
MsgBox "发送内容不能为空", vbOKOnly, "错误"
Else
Dim str As String
str = Text4.Text + vbCrLf
Winsock1.SendData str
Text3.Text = Text3.Text + Format(time, "Long Time") + vbCrLf + str
Text4.ForeColor = &HC0C0C0
Text4.Text = "输入你想要发送的信息"
End If
End Sub
Private Sub Command3_Click()
Winsock1.Connect Text5.Text, CInt(Text6.Text)'开始连接
End Sub
Private Sub Form_Load()
a = 0
isRight = 0
'Winsock1.Connect Text5.Text, CInt(Text6.Text)'开始连接
End Sub
Private Sub Text1_GotFocus()
'Text1.Text = ""
'Text1.ForeColor = vbBlack
End Sub
Private Sub Text1_LostFocus()
If Text1.Text = "" Then
Text1.ForeColor = &HC0C0C0
Text1.Text = "请输入用户名"
End If
End Sub
Private Sub Text2_GotFocus()
Text2.Text = ""
Text2.ForeColor = vbBlack
End Sub
Private Sub Text2_LostFocus()
If Text2.Text = "" Then
Text2.ForeColor = &HC0C0C0
Text2.Text = "请输入密码"
End If
End Sub
Private Sub Text4_GotFocus()
Text4.ForeColor = vbBlack
Text4.Text = ""
End Sub
Private Sub Text4_LostFocus()
If Text4.Text = "" Then
Text4.ForeColor = &HC0C0C0
Text4.Text = "输入你想要发送的信息"
End If
End Sub
Private Sub Text5_GotFocus()
Text5.Text = ""
Text5.ForeColor = vbBlack
End Sub
Private Sub Text5_LostFocus()
If Text5.Text = "" Then
Text5.ForeColor = &HC0C0C0
Text5.Text = "请输入对方IP"
End If
End Sub
Private Sub Text6_GotFocus()
Text6.Text = ""
Text6.ForeColor = vbBlack
End Sub
Private Sub Text6_LostFocus()
If Text6.Text = "" Then
Text6.ForeColor = &HC0C0C0
Text6.Text = "请输入端口号"
End If
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
a = a + 1
Dim isRight As String
If a = 1 Then
Winsock1.GetData isRight
If (isRight) <> "0" Then
Text4.Enabled = True
Text3.Enabled = True
Command2.Enabled = True
Text1.Enabled = False
Text2.Enabled = False
Text5.Enabled = False
Text6.Enabled = False
Command3.Enabled = False
Command1.Enabled = False
End If
Else
Dim str As String
Winsock1.GetData str
Text3.Text = Text3.Text + Format(time, "Long Time") + vbCrLf + str + vbCrLf
End If
End Sub
==服务器端==
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;
TextAreata =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();
}
}
}
}
未连接前
客户端
服务器端
^-^ (无 界面)
连接后
客户端
服务器端
页:
[1]