问题
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 . . .
回答1:
You will get that error if your codes are placed on a network drive. Moving your code to local machine will solve the problem.
来源:https://stackoverflow.com/questions/30783346/new-instance-of-tcpclient-exception