您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。

2026-03-30 13:541阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计472个文字,预计阅读时间需要2分钟。

您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。

服务端使用C#进行开发,引入了以下命名空间:- System- System.Collections.Generic- System.Linq- System.Text- System.Threading- System.Runtime.InteropServices- System.Net- System.Net.Sockets- 命名空间定义:namespace

您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。

服务端

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;

using System.Net;
using System.Net.Sockets;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{

//本机IP
IPAddress ip = IPAddress.Parse("192.168.0.154");
//IP地址跟端口的组合
IPEndPoint iep = new IPEndPoint(ip, 9000);
//创建Socket
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//绑定Socket
socket.Bind(iep);
//服务器已经做好接收任何连接的准备
socket.Listen(10);
//执行accept方法
Socket Client = socket.Accept();
while (true)
{
byte[] message = new byte[1024];
NetworkStream networkStream = new NetworkStream(Client);
int len = networkStream.Read(message, 0, message.Length);
if (len > 0)
{
byte数组转换成string
//string output = System.Text.Encoding.Unicode.GetString(message);
//Console.WriteLine("一共从客户端接收了" + len.ToString() + "字节。接收字符串为:" + output);
}
}
}
}
}

客户端

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using UnityEngine;
class ClientNet
{
private Socket init()
{
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
return clientSocket;
}


public bool Connect(string host,int port)
{
if (m_socket == null)
m_socket = init();
//要连接的远程IP
IPAddress remoteHost = IPAddress.Parse(host);
//IP地址跟端口的组合
IPEndPoint iep = new IPEndPoint(remoteHost, port);
try
{
m_socket.Connect(iep);
return true;
}
catch (System.Exception ex)
{
Debug.LogError("connect error");
return false;
}
}


public void SendData(byte[] bytes)
{
NetworkStream netstream = new NetworkStream(m_socket);
netstream.Write(bytes, 0, bytes.Length);
}


public void CloseSocket()
{
m_socket.Shutdown(SocketShutdown.Both);
m_socket.Close();
}


private Socket m_socket;

private static ClientNet s_instance;
private static readonly byte[] c_staticLocker = new byte[0];
public static ClientNet Instance
{
get
{
if (s_instance == null)
{
lock (c_staticLocker)
{
s_instance = new ClientNet();
}
}
return s_instance;
}
}
}


本文共计472个文字,预计阅读时间需要2分钟。

您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。

服务端使用C#进行开发,引入了以下命名空间:- System- System.Collections.Generic- System.Linq- System.Text- System.Threading- System.Runtime.InteropServices- System.Net- System.Net.Sockets- 命名空间定义:namespace

您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。

服务端

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;

using System.Net;
using System.Net.Sockets;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{

//本机IP
IPAddress ip = IPAddress.Parse("192.168.0.154");
//IP地址跟端口的组合
IPEndPoint iep = new IPEndPoint(ip, 9000);
//创建Socket
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//绑定Socket
socket.Bind(iep);
//服务器已经做好接收任何连接的准备
socket.Listen(10);
//执行accept方法
Socket Client = socket.Accept();
while (true)
{
byte[] message = new byte[1024];
NetworkStream networkStream = new NetworkStream(Client);
int len = networkStream.Read(message, 0, message.Length);
if (len > 0)
{
byte数组转换成string
//string output = System.Text.Encoding.Unicode.GetString(message);
//Console.WriteLine("一共从客户端接收了" + len.ToString() + "字节。接收字符串为:" + output);
}
}
}
}
}

客户端

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using UnityEngine;
class ClientNet
{
private Socket init()
{
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
return clientSocket;
}


public bool Connect(string host,int port)
{
if (m_socket == null)
m_socket = init();
//要连接的远程IP
IPAddress remoteHost = IPAddress.Parse(host);
//IP地址跟端口的组合
IPEndPoint iep = new IPEndPoint(remoteHost, port);
try
{
m_socket.Connect(iep);
return true;
}
catch (System.Exception ex)
{
Debug.LogError("connect error");
return false;
}
}


public void SendData(byte[] bytes)
{
NetworkStream netstream = new NetworkStream(m_socket);
netstream.Write(bytes, 0, bytes.Length);
}


public void CloseSocket()
{
m_socket.Shutdown(SocketShutdown.Both);
m_socket.Close();
}


private Socket m_socket;

private static ClientNet s_instance;
private static readonly byte[] c_staticLocker = new byte[0];
public static ClientNet Instance
{
get
{
if (s_instance == null)
{
lock (c_staticLocker)
{
s_instance = new ClientNet();
}
}
return s_instance;
}
}
}