我在我的应用程序中编写了TCP Server程序,它可以处理传入的连接并解码隐藏的消息。
但是现在我想在Windows上创建一个客户端,它将向服务器发送短字符串消息:“我是客户端”。在服务器中,我使用了class TcpListener,所以我在寻找像TcpSender这样的“相反”类,但是找不到。
服务器ip为:192.168.0.13,端口为13000。
总结了以下问题:如何创建短客户端,从而向服务器发送单条消息?
我的服务器看起来-我发布它只是为了证明我已经做了:
using System;
using System.Net;
using System.Net.Sockets;
usin
最近我一直在使用在多线程上找到的代码来设计我的服务器。
服务器很好地启动并等待客户端,但是当客户端连接时,我会得到这个错误:
通常只允许使用每个套接字地址(协议/网络地址/端口)。
守则:
public partial class Form1 : Form
{
Server s;
public Form1()
{
InitializeComponent();
s = new Server(this);
s.StartTcpServer();
}
private void Stop_Btn_C
我在c#中有一个代码,它从tcp is地址和端口号读取数据,但我的问题是我没有任何带有is地址和端口号的测试平台来测试application.My,这就是我可以在本地环境中测试我的代码的任何方法。
这是我的密码。
public class Listener
{
private TcpListener tcpListener;
private Thread listenThread;
// Set the TcpListener on port 8081.
Int32 port = 8081;
IPAddress localAddr = IPAddre
我正在尝试将TCP协议与System.Net.Sockets库结合使用,让一台计算机向另一台计算机发送字符串。以下是我的主机代码的一部分。侦听器对象应该声明为客户端IP地址还是主机IP地址(如下面的代码所示)?
IPAddress ipAddr = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0]; //Automatically retrieves IPAddress.
int port = 135; //specify port number.
TcpListener listener = new TcpListener(ipAddr
我在C#中使用套接字编程创建了一个简单的服务器,它将从客户端接收一个文件。下面给出了我的示例代码段。
我想增加一些限制。我想限制文件大小(如4KB或2KB)和允许的文件格式(如.doc、.txt、.cpp等)。它将在客户端连接到服务器后立即发送到客户端,以便客户端能够相应地发送文件。我该怎么做?
示例代码段:
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
namespace FileT
最近,我开始学习c#中的Tcp/ip套接字,所以我看了一个教程,并在网上阅读了有关它的内容。我遵循了教程,并制作了客户端-服务器应用程序,将简单的文本发送到同一网络上的服务器。我试图“升级”,使客户端通过服务器与不同网络上的其他客户端进行通信。但是现在服务器无法开始说“请求的地址在其上下文中无效”。 First was server running as localhost, but now when I want to clients that are in different networks communicate through server , server needs to ru
出于学习目的,我开发了一个在本地网络上工作的异步服务器/客户端应用程序。但现在我想让它能够连接到我的公网ip,这样我的服务器就可以从任何地方访问。
以下是服务器代码的相关部分,它似乎工作得很好:
static void Main(string[] args)
{
AsyncServer server = new AsyncServer(60101);
server.RunAsync();
Console.Read();
}
public class AsyncServer
{
privat
这似乎是最显而易见的事情,但我就是想不出如何使用TCPClient和TCPListener获得通过网络发送的字节长度?这是我到目前为止的代码:
'Must listen on correct port- must be same as port client wants to connect on.
Const portNumber As Integer = 9999
Dim tcpListener As New TcpListener(IPAddress.Parse("192.168.2.7"), portNumber)
tcpList
我正在尝试构建一个服务器客户端应用程序(wpf)。
我想要完成的结构是:
只发送数据(简单字符串)的客户端需要是一个wpf应用程序。
只接收数据的客户端(来自发件人客户端的字符串)。
将数据从发送方传输到接收方(可以是多个发送方和多个接收方)的服务器可以是控制台应用程序。
我面临的问题之一是如何分离服务器角色。
第二个问题是如何让服务器知道与其通信的客户端的类型,即发送方或接收方。
这是我的密码:
服务器:
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threadi
我的TCPListener配置如下:
this.tcpListener = new TcpListener(IPAddress.Any, config.portNum);
然后(使用线程)设置如下所示的侦听器函数:
private void ListenForClients()
{
this.tcpListener.Start();
while (true)
{
//blocks until a client has connected to the server
TcpClient client = t
客户端
class Client
{
public TcpClient client;
public StreamWriter server;
public ServerPlayer serverPlayer;
public Player player;
public void Connect(Player p)
{
player = p;
serverPlayer = new ServerPlayer();
client = new TcpClient(AddressFamily.InterNet
我正在尝试编写一个由服务器和一些客户端构建的小程序。服务器将向客户端传输/推送文本消息(不允许客户端向服务器发送文本消息,所有客户端都应该从服务器获得相同的文本消息)--我在这里和网上看到了关于这个主题的一些问题,但没有得到我想要的确切解决方案。大多数解决方案是为每个客户端创建一个线程。但让我们假设,我并不总是知道有多少客户将在某一时间收听。可能是2,也可能是30。我知道有一种方法可以创建线程池,让.NET处理分配线程的问题,但我从来没有对此进行过良好的实践,我对线程的了解也是很好的。现在,我有一个简单的Sever to client程序,它可以像预期的那样工作,但仍然只能使用一个客户端。这是
假设您有以下代码。
this._tcpListener.Start();
while (true)
{
//blocks until a client has connected to the server
TcpClient client = this._tcpListener.AcceptTcpClient();
//create a thread to handle communication
//with connected client
Thread clientThread = new Thread(new Parame
我正在尝试通过TCP连接发送他们的信息后发送文件。在接收主机的末端,接收数据包。我使用Wireshark进行了确认。然而,数据不能在NetworkStream中接收。
public class FileTransporter
{
public void ReceiveFiles(IPAddress IP, int port)
{
TcpListener tcpListener = new TcpListener(IP, port);
tcpListener.Start();
using (TcpClient tcpClient =
我正在制作一个使用NAudio通过TCP/IP连接发送音频的应用程序。
这是我的服务器代码:
// Get server port.
var iServerPort = GetServerPort();
// Initialize memory stream.
_bytes = new List<byte>();
// Initialize tcp listener.
var tcpListener = new TcpListener(IPAddress.Any, iServerPo
下面是c#中的一个示例服务器和客户端代码。服务器将发送一个字符串数组,客户端将接收并显示它,然后客户机将发送一个id,而服务器将接收并显示它。但是在运行这两个服务器时,我在服务器上得到了一个异常。
例外情况如下:
System.ObjectDisposedException类型的未处理异常发生在System.dll中
其他信息:无法访问已释放的对象。
客户端:
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.IO;
using
异步Tcp服务器我只能得到一个客户端。
当我有多个客户端时,我只获取服务器中的第一个客户端,而其他客户端连接到客户端。
下面是我的服务器代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
namespace BeginAcceptTcpClientserver
{
class Program
{
static void Main(string[] args
下面是一个用c#编写的示例服务器程序,它从客户端接收一个id。我的示例代码如下所示。我想向客户端发送一个字符串数组,并在客户端控制台上显示。示例数组字符串可以是:
string[] arr1 = new string[] { "one", "two", "three" };
我需要用下面的服务器和客户端代码添加哪些额外的代码?
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace server
{
class Progra
我使用TCP作为保持生命的机制--这里是我的代码:
客户端
TcpClient keepAliveTcpClient = new TcpClient();
keepAliveTcpClient.Connect(HostId, tcpPort);
//this 'read' is supposed to blocked till a legal disconnect is requested
//or till the server unexpectedly dissapears
int numberOfByptes = keepAliveTcpClient.GetStream
我试图在服务器端发送自定义类型的服务器化对象,并在客户端取消它。我想用Messagepack和tcp套接字来实现这一点。但是,当字节到达客户端时,程序会出现错误:“未能反分区”。类型有两个字段:名称和年龄。
服务器端:
var tcpListener = new TcpListener(IPAddress.Any, 8888);
try
{
tcpListener.Start();
Console.WriteLine("Server is currently run, waiting for incomi
tcpListener = new TcpListener(IPAddress.Any, 6007);
tcpListener.Start();
while (true)
{
TcpClient client = tcpListener.AcceptTcpClient();
sw = new StreamWriter(client.GetStream());
sr = new StreamReader(c
我已经设置了一个PC内部通信服务器/客户端,用于从一个程序到另一个程序发送和接收数据--在本例中,是一个自定义服务器,它正在侦听文本命令和Unity3D。
在大多数情况下,它是有效的,但是每隔一段时间,它就会丢弃数据包,并且如果没有多次尝试,联合将不会得到它们。数据包似乎被发送但丢失了,因为我确实看到了“发送消息”控制台日志。以下是服务器和客户端的代码:
服务器:
class TCPGameServer
{
public event EventHandler Error;
public Action<Data> ADelegate;
TcpListene
想知道如何使这个示例程序更好。具体来说,我应该处理哪些异常?现在它正如期而至。我在声明后加了一个断点:
int numBytesReadFromStream = stream.Read(bytes, 0, bytes.Length);
以检查字节变量中是否包含数据。
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System;
namespace ConsoleApp2
{
class Program
{
public static void Main()
我曾尝试在C#上编写我的第一个异步套接字程序,尽管代码中没有错误,但它不接受连接。我已经通过Telnet查看过了。这是我的TCP服务器代码:我刚刚在互联网上看到了一些示例,但我找不到我的问题,我相信其他东西应该添加到我的代码中。 class Program
{
static byte[] Mybuffer = new byte[4096];
static TcpClient tc = new TcpClient();
private static void PrintUsageInfo()
{
}
static void Main
在使用TcpClient和TcpListener时抛出这个异常,我有点不知所措。它第一次工作时,我再次运行它,得到以下异常:
通常只允许使用一个套接字地址(协议/网络地址/端口) 127.0.0.1:8086
我检查过了以确保我正在关闭任何打开的连接。我尝试过手动调用TcpClient上的close,以及使用IDisposable使用模式,但仍然存在相同的问题。
下面是代码,如果您在Visual中复制粘贴它,它应该只运行(前提是您已经使用语句添加了以下内容)
using System;
using System.IO;
using System.Net;
using System.Net.So