VB + Winsock + CGI 实现 QQ (OICQ) 在线检测

VB + Winsock + CGI 实现 QQ (OICQ) 在线检测(支持代理服务器)!
标准 EXE 例程下载
http://microinfo.top263.net/Zip/WskQQExe.zip

'请先 "引用" -> "浏览" -> "Windows 目录/SYSTEM/MSWINSCK.OCX"
Option Explicit
Dim sResponse As String
Dim WithEvents WinsockX As MSWinsockLib.Winsock
Dim WithEvents WinsockListenX As MSWinsockLib.Winsock
Private Sub Check1_Click()
Text2.Enabled = VBA.IIf(Check1.Value = vbChecked, True, False)
Text3.Enabled = Text2.Enabled
End Sub
Private Sub Check2_Click()
If Check2.Value = vbChecked Then
   Text4.Enabled = False
   WinsockListenX.Protocol = sckTCPProtocol
   WinsockListenX.LocalPort = CInt(Text4.Text)
   WinsockListenX.Listen
Else
   Text4.Enabled = True
   If WinsockX.State <> sckClosed Then
      WinsockX.Close
   End If
   If WinsockListenX.State <> sckClosed Then
      WinsockListenX.Close
   End If
End If
End Sub
Private Sub Command1_Click()
sResponse = ""
Command1.Enabled = False
Me.MousePointer = vbHourglass
Dim i As Long
If WinsockX.State <> sckClosed Then
   WinsockX.Close
End If
WinsockX.Protocol = sckTCPProtocol
If Check1.Value = vbChecked Then
   WinsockX.Connect Trim(Text2.Text), CInt(Text3.Text)
Else
   WinsockX.Connect "search.tencent.com", 80
End If
Do Until WinsockX.State = sckConnected
   DoEvents
   i = i + 1
   If i > 50000 Then
      If VBA.MsgBox("TimeOut,Retry ", vbQuestion + vbYesNo) = vbYes Then
         i = 0
      Else
         Command1.Enabled = True
         Me.MousePointer = vbDefault
         Exit Sub
      End If
   End If
Loop
WinsockX.SendData "POST " & VBA.IIf(Check1.Value = vbChecked, "HTTP://search.tencent.com", "") & "/cgi-bin/friend/oicq_find HTTP/1.1" & vbCrLf _
                & "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/vnd.ms-powerpoint, */*" & vbCrLf _
                & "Accept -Language: zh -cn" & vbCrLf _
                & "Content-Type: application/x-www-form-urlencoded" & vbCrLf _
                & "Accept -Encoding: gzip , deflate" & vbCrLf _
                & "User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Win 9x 4.90)" & vbCrLf _
                & "Host: " & WinsockX.RemoteHost & vbCrLf _
                & "Content-Length: " & VBA.Len(VBA.Trim("oicq_no=" & VBA.Trim(Text1.Text) & "&mov=0&begnum=0")) & vbCrLf _
                & "Connection: Keep -Alive" & vbCrLf _
                & "Cookie: 3wave=1" & vbCrLf & vbCrLf _
                & "oicq_no=" & VBA.Trim(Text1.Text) & "&mov=0&begnum=0"
End Sub
Private Sub Form_Load()
Text1.Text = "6881818"
Text2.Text = "192.168.0.1"
Text3.Text = "8080"
Text4.Text = "80"
Set WinsockX = New MSWinsockLib.Winsock
Set WinsockListenX = New MSWinsockLib.Winsock
Check1_Click
Check2_Click
End Sub
Private Sub WinsockListenX_ConnectionRequest(ByVal requestID As Long)
If WinsockX.State <> sckClosed Then
   WinsockX.Close
End If
WinsockX.Accept requestID
End Sub
Private Sub WinsockX_Close()
Command1.Enabled = True
Me.MousePointer = vbDefault
If sResponse Like "*http://img.tencent.com/face/*-3.gif*" Then
   MsgBox "Off line!"
ElseIf sResponse Like "*http://img.tencent.com/face/*-2.gif*" Then
   MsgBox "On line!"
ElseIf sResponse Like "*http://img.tencent.com/face/*-1.gif*" Then
   MsgBox "Hide!"
End If
End Sub
Private Sub WinsockX_DataArrival(ByVal bytesTotal As Long)
Dim s As String
WinsockX.GetData s, vbString
If Check2.Value = vbChecked Then
   MsgBox s
End If
sResponse = sResponse & s
End Sub

ActiveX DLL 例程下载:
http://microinfo.top263.net/Zip/WskQQDll.zip

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本文将介绍如何使用MFC Winsock实现基于TCP协议的C/S聊天程序。 1. 创建MFC应用程序 首先,我们需要创建一个MFC应用程序。在创建向导中选择“单文档应用程序”类型,勾选“包含MFC的ActiveX控件”和“支持ActiveX控件”,其他选项默认即可。 2. 添加界面元素 在资源视图中添加两个编辑框和一个按钮,用于输入和显示聊天内容,以及发送消息。 3. 编写代码 在Dlg.cpp文件中添加以下代码: 在头文件中添加以下头文件: #include "stdafx.h" #include "Dlg.h" #include "afxdialogex.h" #include <winsock2.h> #pragma comment(lib, "ws2_32.lib") 在OnInitDialog()函数中添加以下代码: WSADATA wsaData; SOCKET ConnectSocket = INVALID_SOCKET; struct addrinfo *result = NULL, *ptr = NULL, hints; int iResult; // Initialize Winsock iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != 0) { MessageBox(_T("WSAStartup failed with error: %d"), iResult); return FALSE; } ZeroMemory(&hints, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; // Resolve the server address and port iResult = getaddrinfo(_T("localhost"), _T("27015"), &hints, &result); if ( iResult != 0 ) { MessageBox(_T("getaddrinfo failed with error: %d"), iResult); WSACleanup(); return FALSE; } // Attempt to connect to an address until one succeeds for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) { // Create a SOCKET for connecting to server ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol); if (ConnectSocket == INVALID_SOCKET) { MessageBox(_T("socket failed with error: %ld\n"), WSAGetLastError()); WSACleanup(); return FALSE; } // Connect to server. iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen); if (iResult == SOCKET_ERROR) { closesocket(ConnectSocket); ConnectSocket = INVALID_SOCKET; continue; } break; } freeaddrinfo(result); if (ConnectSocket == INVALID_SOCKET) { MessageBox(_T("Unable to connect to server!")); WSACleanup(); return FALSE; } // Set the mode of the socket to be nonblocking u_long iMode = 1; iResult = ioctlsocket(ConnectSocket, FIONBIO, &iMode); if (iResult == SOCKET_ERROR) { MessageBox(_T("ioctlsocket failed with error: %d"), WSAGetLastError()); closesocket(ConnectSocket); WSACleanup(); return FALSE; } // Disable Nagle's algorithm char value = 1; setsockopt(ConnectSocket, IPPROTO_TCP, TCP_NODELAY, &value, sizeof(value)); 在OnDestroy()函数中添加以下代码: // shutdown the connection since no more data will be sent iResult = shutdown(ConnectSocket, SD_SEND); if (iResult == SOCKET_ERROR) { MessageBox(_T("shutdown failed with error: %d"), WSAGetLastError()); closesocket(ConnectSocket); WSACleanup(); return FALSE; } // cleanup closesocket(ConnectSocket); WSACleanup(); 在OnBnClickedButtonSend()函数中添加以下代码: // Send a message CString strMsg; m_edtMsg.GetWindowText(strMsg); char buf[1024]; strcpy_s(buf, CT2A(strMsg.GetBuffer())); iResult = send(ConnectSocket, buf, strlen(buf), 0); if (iResult == SOCKET_ERROR) { MessageBox(_T("send failed with error: %d"), WSAGetLastError()); closesocket(ConnectSocket); WSACleanup(); return FALSE; } m_lstMsg.AddString(strMsg); m_edtMsg.SetWindowText(_T("")); 在OnReceive()函数中添加以下代码: char buf[1024]; int iResult = recv(ConnectSocket, buf, sizeof(buf), 0); if (iResult > 0) { CString strMsg(buf); m_lstMsg.AddString(strMsg); } else if (iResult == 0) { MessageBox(_T("Connection closed")); closesocket(ConnectSocket); WSACleanup(); return FALSE; } else { int err = WSAGetLastError(); if (err != WSAEWOULDBLOCK) { MessageBox(_T("recv failed with error: %d"), err); closesocket(ConnectSocket); WSACleanup(); return FALSE; } } 4. 编译运行 现在我们可以编译运行程序,在输入框中输入消息并点击发送按钮,就可以发送消息到服务器并在列表框中显示。当接收到服务器返回的消息时,也会在列表框中显示。 注意:本程序仅实现了客户端的代码,需要和服务器端代码配合使用才能实现完整的C/S聊天程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值