C#.NET客户端与C服务器之间的Socket编程
在这里,我想问一下如何通过socket编程在客户端(在Windows上,使用C#.NET)和服务器(在Ubuntu上,使用C)之间创建通信。 如果服务器客户端都是C#.NET或C,但不知道如何按照我的要求执行,那么我已经成功地完成了。 我试图在几个网站上查找信息,但找不到确切的信息。 我感谢你的评论。多谢各位 下面是关于客户端的简单代码C#.NET客户端与C服务器之间的Socket编程,c#,c,sockets,C#,C,Sockets,在这里,我想问一下如何通过socket编程在客户端(在Windows上,使用C#.NET)和服务器(在Ubuntu上,使用C)之间创建通信。 如果服务器客户端都是C#.NET或C,但不知道如何按照我的要求执行,那么我已经成功地完成了。 我试图在几个网站上查找信息,但找不到确切的信息。 我感谢你的评论。多谢各位 下面是关于客户端的简单代码 using System; using System.Collections.Generic; using System.ComponentModel; usi
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace WindowsFormsApplication9
{
public partial class Form1 : Form
{
// Receiving byte array
byte[] bytes = new byte[1024];
Socket senderSock;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btn_connect_Click(object sender, EventArgs e)
{
try
{
// Create one SocketPermission for socket access restrictions
SocketPermission permission = new SocketPermission(
NetworkAccess.Connect, // Connection permission
TransportType.Tcp, // Defines transport types
"", // Gets the IP addresses
SocketPermission.AllPorts // All ports
);
// Ensures the code to have permission to access a Socket
permission.Demand();
// Resolves a host name to an IPHostEntry instance
IPHostEntry ipHost = Dns.GetHostEntry("");
// Gets first IP address associated with a localhost
IPAddress ipAddr = ipHost.AddressList[0];
// Creates a network endpoint
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 4510);
// Create one Socket object to setup Tcp connection
senderSock = new Socket(
ipAddr.AddressFamily,// Specifies the addressing scheme
SocketType.Stream, // The type of socket
ProtocolType.Tcp // Specifies the protocols
);
senderSock.NoDelay = false; // Using the Nagle algorithm
// Establishes a connection to a remote host
senderSock.Connect(ipEndPoint);
}
catch (Exception exc) { MessageBox.Show(exc.ToString()); }
}
private void btnSend_Click(object sender, EventArgs e)
{
try
{
// Sending message
//<Client Quit> is the sign for end of data
string theMessageToSend = text_IP.Text;
byte[] msg = Encoding.Unicode.GetBytes(theMessageToSend + "<Client Quit>");
// Sends data to a connected Socket.
int bytesSend = senderSock.Send(msg);
}
catch (Exception exc) { MessageBox.Show(exc.ToString()); }
}
}
}
套接字是一个普遍的概念,不是任何语言所特有的。 您可以继续使用相同的C Ubuntu服务器代码和C#Windows客户端代码 您需要注意的几件事是:
- 如何绑定服务器和客户端以及使用的协议
- 防火墙设置
快乐编码:)所以我假设你的客户至少可以互相交谈。但您的问题似乎是,您从C#以Unicode格式发送文本,然后在客户端以ASCII格式接收文本。除非“C”客户端的默认值不是ASCII。在客户端代码中,您可以使用空主机名调用
GetHostEntry
。这将为您提供客户端的IP地址
您应该使用服务器的主机名调用GetHostEntry
:
// Resolves a host name to an IPHostEntry instance
IPHostEntry ipHost = Dns.GetHostEntry("Server.Host.Name");
// Gets first IP address associated with a localhost
IPAddress ipAddr = ipHost.AddressList[0];
// Creates a network endpoint
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 4510);
您是否有理由怀疑C/C中的套接字代码在C/C中的客户端/服务器上是否正常工作?如果是这样的话,我们怎么能在没有看到代码的情况下知道这种怀疑的根源呢?对不起,我忘了附上代码了。我编辑并附加了客户代码。对于服务器代码,我确实有一个示例程序,但它不适用于此客户机代码。客户机代码与C#.NET中的服务器代码配合使用。您能告诉我们哪种方式不起作用吗?你能连接到服务器吗?您是否收到一些错误消息/异常?在客户端代码中,使用空主机名调用GetHostEntry。您在测试时是否使用了服务器的主机名?我无法连接到服务器。服务器没有响应。我已使用System.Text.Encoding.ASCII.GetBytes将代码从Unicode更改为ASCII。我最大的问题是,我不知道如何用C为服务器创建正确的代码。我有一些示例,并尝试对其进行修改,但仍然无法工作。最后,我无法连接到服务器并发送数据。无论如何,谢谢你的评论:)谢谢你的评论。我正在尝试为Windows上的服务器重新生成代码。希望它能很快起作用:你可以尝试在windows上运行C服务器端代码。尝试在windows上运行服务器和客户端。
// Resolves a host name to an IPHostEntry instance
IPHostEntry ipHost = Dns.GetHostEntry("Server.Host.Name");
// Gets first IP address associated with a localhost
IPAddress ipAddr = ipHost.AddressList[0];
// Creates a network endpoint
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 4510);