c# 如何实现端口数据的 通信
查阅好多资料也不没看明白怎么操作 '
我只需要向端口发送数据 和接受数据
服务端协议 已经早就存在
是 delphi 开发的
不知道 是否存在语言的对接问题?
还有MSDN 关于socket的代码 我也搞不清楚 怎么用
比如 ---------------------
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
public class GetSocket
{
private static Socket ConnectSocket(string server, int port)
{
Socket s = null;
IPHostEntry hostEntry = null;
// Get host related information.
hostEntry = Dns.GetHostEntry(server);
// Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
// an exception that occurs when the host IP Address is not compatible with the address family
// (typical in the IPv6 case).
foreach(IPAddress address in hostEntry.AddressList)
{
IPEndPoint ipe = new IPEndPoint(address, port);
Socket tempSocket =
new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
tempSocket.Connect(ipe);
if(tempSocket.Connected)
{
s = tempSocket;
break;
}
else
{
continue;
}
}
return s;
}
// This method requests the home page content for the specified server.
private static string SocketSendReceive(string server, int port)
{
string request = "GET / HTTP/1.1\r\nHost: " + server +
"\r\nConnection: Close\r\n\r\n";
Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
Byte[] bytesReceived = new Byte[256];
// Create a socket connection with the specified server and port.
Socket s = ConnectSocket(server, port);
if (s == null)
return ("Connection failed");
// Send request to the server.
s.Send(bytesSent, bytesSent.Length, 0);
// Receive the server home page content.
int bytes = 0;
string page = "Default HTML page on " + server + ":\r\n";
// The following will block until te page is transmitted.
do {
bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
page = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes);
}
while (bytes > 0);
return page;
}
public static void Main(string[] args)
{
string host;
int port = 80;
if (args.Length == 0)
// If no server name is passed as argument to this program,
// use the current host name as the default.
host = Dns.GetHostName();
else
host = args[0];
string result = SocketSendReceive(host, port);
Console.WriteLine(result);
}
-------------------------------------------------------------------------------------
我本地一直没有是通 不知道效果是怎么样的
希望大家给予指导谢谢
我只需要向端口发送数据 和接受数据
服务端协议 已经早就存在
是 delphi 开发的
不知道 是否存在语言的对接问题?
还有MSDN 关于socket的代码 我也搞不清楚 怎么用
比如 ---------------------
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
public class GetSocket
{
private static Socket ConnectSocket(string server, int port)
{
Socket s = null;
IPHostEntry hostEntry = null;
// Get host related information.
hostEntry = Dns.GetHostEntry(server);
// Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
// an exception that occurs when the host IP Address is not compatible with the address family
// (typical in the IPv6 case).
foreach(IPAddress address in hostEntry.AddressList)
{
IPEndPoint ipe = new IPEndPoint(address, port);
Socket tempSocket =
new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
tempSocket.Connect(ipe);
if(tempSocket.Connected)
{
s = tempSocket;
break;
}
else
{
continue;
}
}
return s;
}
// This method requests the home page content for the specified server.
private static string SocketSendReceive(string server, int port)
{
string request = "GET / HTTP/1.1\r\nHost: " + server +
"\r\nConnection: Close\r\n\r\n";
Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
Byte[] bytesReceived = new Byte[256];
// Create a socket connection with the specified server and port.
Socket s = ConnectSocket(server, port);
if (s == null)
return ("Connection failed");
// Send request to the server.
s.Send(bytesSent, bytesSent.Length, 0);
// Receive the server home page content.
int bytes = 0;
string page = "Default HTML page on " + server + ":\r\n";
// The following will block until te page is transmitted.
do {
bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
page = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes);
}
while (bytes > 0);
return page;
}
public static void Main(string[] args)
{
string host;
int port = 80;
if (args.Length == 0)
// If no server name is passed as argument to this program,
// use the current host name as the default.
host = Dns.GetHostName();
else
host = args[0];
string result = SocketSendReceive(host, port);
Console.WriteLine(result);
}
-------------------------------------------------------------------------------------
我本地一直没有是通 不知道效果是怎么样的
希望大家给予指导谢谢
...全文
相关推荐
c#向指定端口发送数据,监听指定端口
c#向指定端口发送数据,监听指定端口,使用端口监听获取数据信息,向指定端口发送需要的数据
C#中Socket实现UDP通信的详细指南
网络通信是计算机网络中数据交换的方式,对于现代软件开发至关重要。它涉及数据如何在网络中的不同计算机或设备间传输,而C#作为一种强大的编程语言,提供了多种工具和库来支持网络通信的实现。多播是一种网络通信方式,允许将信息从一个发送者发送到多个接收者。这对于某些应用来说非常有用,比如分布式系统中的服务发现。
基于C#和三菱PLC通过MX Component进行通信的具体方法
基于C#和三菱PLC通过MX Component进行通信的具体方法
C#的Socket实现UDP协议通信
C#的Socket实现UDP协议通信 CSharp
UR机器人29999端口通信C#例程
UR机器人29999端口可用来发送一些简单的功能指令,本例子编写了简单的端口通信类,用于实现29999端口功能,代码基于C#。另外附上29999端口的说明书做参考


