的TcpClient异常的新实例 [英] New Instance of TCPClient Exception

查看:301
本文介绍了的TcpClient异常的新实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我前几天问这个类似的问题。在这一点上,我试图用插座。目前,我使用的TcpClient做肮脏的插座为我工作。我使用Windows 7和视觉工作室2013专业版



原件和电流项目概况:



我需要创建一个应用程序(使用C#的WinForms),允许用户连接到设备(硬件设备,将参数/命令发送)通过WiFi(WiFi是最终目标,但我目前解决任何连接),然后将邮件发送到该装置。我知道一些C#和一些插座/ IP的东西,但不使用插座/ IP C#。该方案的可视化,图形用户界面一边是不是我与挣扎。我似乎无法得到的TcpClient启动和运行,并进行任何真正的联系。我不断收到一个无效的参数提供异常。



在这个问题上的任何提示或C#网络/的TcpClient /等帮助。 。欢迎



我试图创建一个新的TcpClient每一没有成功以下几种方式:

 的TcpClient的TcpClient =新的TcpClient(); 
的TcpClient的TcpClient =新的TcpClient(家庭); //家庭是一个创造AddressFamily
的TcpClient的TcpClient =新的TcpClient(端ClientIPAddress,clientPortNumber);
的TcpClient的TcpClient =新的TcpClient(remoteEP); // remoteEP是由我建立了一个远程端点
的TcpClient的TcpClient =新的TcpClient(localEP); // localEP是当地的终点

每个这些是的TcpClient类中构造函数之一。每一个这些给了我以下异常:
一个无效的参数提供



堆栈跟踪:




在System.Net.Sockets.Socket..ctor(AddressFamily addressFamily,socketType socketType,ProtocolType protocolType)\r\\\
在System.Net.Sockets.TcpClient .initialize()\r\\\
在System.Net.Sockets.TcpClient..ctor(AddressFamily家族)\r\\\
在System.Net.Sockets.TcpClient..ctor()\r\\\
在ConnectLite.ConnectLite.MakeConnection()在H:\Howard\Projects\ConnectLite\ConnectLite\Form1.cs:线​​120




我拥有所有必要的进口和usings的。



这问题时,我尝试运行在不同的计算机上的应用程序变得奇怪。在单独的计算机,无论是运行应用程序,而不从调试文件视觉工作室,或者使用视觉工作室2013年不同的计算机上运行它,应用程序需要我进一步比异常,异常抛出永远。



我只是尝试使用的TcpClient做一些简单的信息发送。该的TcpClient,但是,阻止我这样做。



当我使用套接字被,它是创造一个新的socket时抛出同样的异常。



 插座ClientSocket的= 
新的Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);



做任何你对此事有何看法?



要简化,我已经把代码相关的块,放入一个控制台应用程序,从而很容易读



与发行代码简单的块:

 使用系统; 
使用System.Collections.Generic;
使用System.Linq的;使用System.Net
;使用的System.Net.Sockets
;
使用System.Text;使用System.Threading.Tasks
;

命名空间ConsoleTestConnectLite
{
类节目
{
静态无效的主要(字串[] args)
{

{
的TcpClient的TcpClient =新的TcpClient();
}
赶上(SocketException E)
{
Console.WriteLine(e.Message);
Console.WriteLine(错误:+ e.StackTrace);
Console.WriteLine(e.Message +错误代码:+ e.ErrorCode.ToString());
}
}
}
}



控制台输出:

 一个无效的参数提供
错误:在System.Net.Sockets.Socket..ctor(AddressFamily addressFamily,插座
型socketType,protocolType protocolType)
在System.Net.Sockets.TcpClient.initialize()
在System.Net.Sockets.TcpClient..ctor(AddressFamily家庭)
在System.Net.Sockets.TcpClient..ctor()在ConsoleTestConnectLite.Program.Main
(字串[] args)在H:\Howard\Projects\ConsoleTestConnectLite\ConsoleTestConnectLite\Program。 CS:行17
一个无效的参数提供。错误代码:10022
按任意键继续。 。 。


解决方案

您会得到这个错误,如果你的代码被放置在网络驱动器。移动你的代码到本地机器就能解决问题。


I asked a similar question to this a few days ago. At that point, I was trying to use sockets. Currently, I am using TCPClient to do the dirty socket work for me. I am using Windows 7 and Visual studios 2013 Professional.

Original and Current Project Outline:

I need to create an application (WinForms using C#) that allows the user to connect to a device (a piece of hardware that will be sent arguments/commands) via wifi (wifi is end goal but I am settling at the moment for any connection) and then send messages to said device. I know some C# and some sockets/IP stuff, but not sockets/IP using C#. The visual, GUI side of the program is not what I am struggling with. I cannot seem to get the TCPClient up and running and make any real connection. I keep getting the "An Invalid Argument was Supplied" exception.

Any tips on this particular issue or help with C# networking/TCPClient/etc. are welcome.

I have tried to create a new TcpClient each of the following ways without success:

TcpClient tcpClient = new TcpClient();
TcpClient tcpClient = new TcpClient(family); // family is a created AddressFamily
TcpClient tcpClient = new TcpClient(clientIPAddress, clientPortNumber);
TcpClient tcpClient = new TcpClient(remoteEP); // remoteEP is a remote end point created by me
TcpClient tcpClient = new TcpClient(localEP); // localEP is a local end point

Each of these is one of the constructors within the TCPClient class. Each of these gives me the following exception: An Invalid Argument was Supplied

Stack Trace:

at System.Net.Sockets.Socket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)\r\n at System.Net.Sockets.TcpClient.initialize()\r\n at System.Net.Sockets.TcpClient..ctor(AddressFamily family)\r\n at System.Net.Sockets.TcpClient..ctor()\r\n at ConnectLite.ConnectLite.MakeConnection() in h:\Howard\Projects\ConnectLite\ConnectLite\Form1.cs:line 120

I do have all of the necessary imports and usings.

This problem gets strange when I try to run the application on a different computer. On separate computers, whether it be running the application without Visual Studios from the debug file, or running it on a different computer using Visual Studios 2013, the application takes me further than the exception and the exception is never thrown.

I am simply trying to use TCPClient to do some simple message sending. The TCPClient, however, is preventing me from doing that.

When I had been using sockets, it was throwing the same exception when creating a new socket.

Socket clientSocket =
new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

Do any of you have any insights on this matter?

To simplify, I have put the pertinent pieces of code into a console app so as to be easily read.

Simple block of code with issue:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleTestConnectLite
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                TcpClient tcpClient = new TcpClient();
            }
            catch (SocketException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine("Error: " + e.StackTrace);
                Console.WriteLine(e.Message + ". Error Code: " + e.ErrorCode.ToString());
            }
        }
    }
}

Console Output:

An invalid argument was supplied
Error:    at System.Net.Sockets.Socket..ctor(AddressFamily addressFamily, Socket
Type socketType, ProtocolType protocolType)
   at System.Net.Sockets.TcpClient.initialize()
   at System.Net.Sockets.TcpClient..ctor(AddressFamily family)
   at System.Net.Sockets.TcpClient..ctor()
   at ConsoleTestConnectLite.Program.Main(String[] args) in h:\Howard\Projects\ConsoleTestConnectLite\ConsoleTestConnectLite\Program.cs:line 17
An invalid argument was supplied. Error Code: 10022
Press any key to continue . . .

解决方案

You will get that error if your codes are placed on a network drive. Moving your code to local machine will solve the problem.

这篇关于的TcpClient异常的新实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆