问题:对于初学者来说,让两个程序以不同语言运行并在不同计算机上运行以便相互发送简单消息的最快/最简单方法是什么?
我的详细信息:我有两台计算机,一台运行Visual C ++中的程序,另一台运行Visual Basic程序(两台都来自visual studio 2013,虽然我可能需要这样做一些较旧的Visual Basic代码,在.NET时代之前)。由于硬件和遗留原因的结合,它们以不同的语言运行。通信很简单:一个简单的二进制触发器(开/关信号)可以工作,或者说一个字符,单词或字符串很容易描述程序的状态。
我尝试过的事情:我对通信协议和这类事情知之甚少,但我知道“套接字”编程可能是进行此类通信的一种简单方法。听起来TCP协议对我很好。不幸的是,我见过的所有例子都使用相同的编程语言进行通信(例如C ++到C ++或基本到基本)。我也理解另一种选择可能是从C ++中运行一些基本代码,反之亦然,但这似乎是一个有点麻烦的解决方案。
使用一些在线指南,当我们使用C ++(通过Winsock工具)时,我已经能够让我的两台计算机相互通信,但当我尝试使用Visual Basic客户端混合C ++服务器时,他们无法联系。我正在使用的代码,该代码在以下网站(https://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient(v=vs.110).aspx)和(https://msdn.microsoft.com/en-us/library/windows/desktop/ms738545(v=vs.85).aspx)中被大量劫持并略有修改 在下面,如果你想看看它。
摘要问题:
Visual Basic TCP客户端:
Imports System
Imports System.Text
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports Microsoft.VisualBasic
Public Class GetSocket
Shared Sub Connect(server As [String], message As [String])
Try
' Create a TcpClient.
' Note, for this client to work you need to have a TcpServer
' connected to the same address as specified by the server, port
' combination.
Dim port As Int32 = 27015
Dim client As New TcpClient(server, port)
' Translate the passed message into ASCII and store it as a Byte array.
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)
' Get a client stream for reading and writing.
' Stream stream = client.GetStream();
Dim stream As NetworkStream = client.GetStream()
' Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length)
Console.WriteLine("Sent: {0}", message)
' Receive the TcpServer.response.
' Buffer to store the response bytes.
data = New [Byte](256) {}
' String to store the response ASCII representation.
Dim responseData As [String] = [String].Empty
' Read the first batch of the TcpServer response bytes.
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
Console.WriteLine("Received: {0}", responseData)
' Close everything.
stream.Close()
client.Close()
Catch e As ArgumentNullException
Console.WriteLine("ArgumentNullException: {0}", e)
Catch e As SocketException
Console.WriteLine("SocketException: {0}", e)
End Try
Console.WriteLine(ControlChars.Cr + " Press Enter to continue...")
Console.Read()
End Sub 'Connect
Public Shared Sub Main()
Dim host As String = "192.168.107.254"
Dim message As String = "I SPEAK TO YOU FROM THE OTHER SIDE"
Connect(host, message)
End Sub 'Main
' WHY ARE COMMENTS SO WEIRD IN BASIC
End Class
Visual C ++服务器:
#include "stdafx.h"
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#pragma comment(lib, "Ws2_32.lib")
#define DEFAULT_PORT "27015"
#define DEFAULT_BUFLEN 512
int main() {
WSADATA wsaData;
int iResult;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
getchar();
return 1;
}
struct addrinfo *result = NULL, *ptr = NULL, hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
// Resolve the local address and port to be used by the server
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
if (iResult != 0) {
printf("getaddrinfo failed: %d\n", iResult);
WSACleanup();
getchar();
return 1;
}
SOCKET ListenSocket = INVALID_SOCKET;
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
printf("Error at socket(): %ld\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
getchar();
return 1;
}
// Setup the TCP listening socket
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("bind failed with error: %d\n", WSAGetLastError());
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
getchar();
return 1;
}
freeaddrinfo(result);
if (listen(ListenSocket, SOMAXCONN) == SOCKET_ERROR) {
printf("Listen failed with error: %ld\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
SOCKET ClientSocket;
ClientSocket = INVALID_SOCKET;
// Accept a client socket
ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
printf("accept failed: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
char recvbuf[DEFAULT_BUFLEN];
int iSendResult;
int recvbuflen = DEFAULT_BUFLEN;
// Receive until the peer shuts down the connection
do {
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
if (iResult > 0) {
printf("Bytes received: %d\n", iResult);
printf("recieved message: %s\n", recvbuf);
// Echo the buffer back to the sender
iSendResult = send(ClientSocket, recvbuf, iResult, 0);
if (iSendResult == SOCKET_ERROR) {
printf("send failed: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
printf("Bytes sent: %d\n", iSendResult);
}
else if (iResult == 0)
printf("Connection closing...\n");
else {
printf("recv failed: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
} while (iResult > 0);
// shutdown the send half of the connection since no more data will be sent
iResult = shutdown(ClientSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
// cleanup
closesocket(ClientSocket);
WSACleanup();
printf("Success!");
getchar();
return 0;
}
答案 0 :(得分:1)
是Visual C ++和Visual Basic之间的直接套接字通信 可能的?
是
有更好的选择吗?
可能,取决于......
我是白痴吗?
此时未知。你也用PHP / jQuery编程吗?
Is there somewhere a beginner should go to learn about these types of things? Let me know if there is anything I can do to clarify the question.
在SO上的插座编程中有Q&amp; A的LOADS:)