3

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 . . .
halfer
  • 19,824
  • 17
  • 99
  • 186
RHoward78
  • 53
  • 1
  • 9
  • It would help if you'd show a short but complete console app (far less going on that way) with sample values so that we can reproduce this more easily. Currently we don't know about the port numbers etc. – Jon Skeet Jun 11 '15 at 14:18
  • @JonSkeet I am not using a console application. I will add more pertinent code to that listed above. I feel that the problem is not dictated by the port/ip addresses because I get the exception when I use the 0-argument constructor of TcpClient: `TcpClient tcpClient = new TcpClient();` – RHoward78 Jun 11 '15 at 14:24
  • There's pretty much nothing to know about a console app - you just put the code you want in there. There's no need to have a GUI involved here - you can provide a short but complete program in less than 10 lines with a console app. It would also help if you'd post the complete stack trace, not just the message. – Jon Skeet Jun 11 '15 at 14:28
  • 1
    It sounds like your machine may have some serious issues - if even that socket construction code doesn't work, it sounds like it's in a bad state. – Jon Skeet Jun 11 '15 at 14:34
  • @JonSkeet So in the console app, just add the pertinent code that I am having issues with? – RHoward78 Jun 11 '15 at 14:34
  • Yes, in the `Main` method. It looks like you should be able to demonstrate it with *just* `var client = new TcpClient();`. You can then try to run that console app. – Jon Skeet Jun 11 '15 at 14:34
  • @JonSkeet What do you mean by in a serious state? And yeah, I am beginning to think the same thing. Everything else seems to work fine. – RHoward78 Jun 11 '15 at 14:35
  • Well, I mean that your .NET installation could be corrupt, or there could be some issue with your machine's network stack. Try to reproduce in a console app, then try rebooting, then try looking in device manager for any oddities... – Jon Skeet Jun 11 '15 at 14:36
  • Alright man, I will start there and edit/comment when I finish that list. Thank you for your time! I may talk to you soon. – RHoward78 Jun 11 '15 at 14:42
  • @JonSkeet Just put it into a console application. How would you like for me to show this Console App code and the output that the window gave me? – RHoward78 Jun 11 '15 at 14:56
  • I'd replace *all* of the code in your question with just the code in your console app - and put the output in another code block in the same way. See http://stackoverflow.com/questions/11593615 for an example. – Jon Skeet Jun 11 '15 at 15:04
  • @JonSkeet Console App code now shown along with console window output. – RHoward78 Jun 11 '15 at 15:40
  • That's still *far* longer than it needs to be. Does a Main method with a single line body of `TcpClient tcpClient = new TcpClient();` show the same issue? I strongly suspect so. Given that you're getting an exception before you connect, you can kill all of that code. You don't need to catch the exception, because a console app will log the error anyway. – Jon Skeet Jun 11 '15 at 15:41
  • But fundamentally, that code *doesn't* fail on my machine - again, suggesting there's a problem with your .NET installation or Windows itself, I'm afraid. – Jon Skeet Jun 11 '15 at 15:42
  • Your machine is hosed. `new TcpClient()` cannot fail. Try to reset the Winsock catalog and reinstall .NET. This is about the only option that I can think of. Maybe use a Procmon trace to look for something that failed. – usr Jun 11 '15 at 15:44
  • @JonSkeet Edit:Simplified. You will have to forgive me, I just started using StackOverflow last week. – RHoward78 Jun 11 '15 at 15:48
  • So between you, @JonSkeet and you, usr, what do y'all suggest to fix my internal, nagging issue? – RHoward78 Jun 11 '15 at 15:50
  • 1
    As I mentioned before, reboot, reinstall .NET, check for any driver issues... but at this point it's not really a "programming C#" issue :( – Jon Skeet Jun 11 '15 at 15:51
  • Maybe you are lucky and it is a .NET problem. You seem to be able to use other networking software so at least it is possible to open a socket and a TCP connection. – usr Jun 11 '15 at 15:54
  • @JonSkeet So far, Rebooting did not work. How do I check for driver issues? – RHoward78 Jun 11 '15 at 16:30
  • At this point, you'd be better off on SuperUser. But you'd bring up DeviceManager... – Jon Skeet Jun 11 '15 at 16:32
  • So many questions, I know haha. How do I ask this question on SuperUser? Do I still post code? – RHoward78 Jun 11 '15 at 16:47
  • Also, I have the device manager up, I just do not really know what I am looking for. @JonSkeet – RHoward78 Jun 11 '15 at 16:49
  • You could post code, yes - just the shortest console app you can. (You can strip the example code down further. Get rid of the namespace. Get rid of the unnecessary using directives. Get rid of the catch block.) Make it clear that this is merely the way that you've diagnosed that there's something incorrect on your system - that you're not asking a coding question as such, you're asking for help diagnosing the underlying cause. (You might want to link to this question, but it will probably be closed and deleted over time.) – Jon Skeet Jun 11 '15 at 16:51
  • Well then, time to debug the framework sources, no? Areas of interest are `Socket`’s constructor as well as the `SafeCloseSocket` class, which creates the actual Winsock socket. The very important error code is missing from the question, please update your question to include it. – Daniel B Jun 11 '15 at 17:29
  • @DanielB What error code are you speaking of? Is the one that I show in the console window output the one you are looking for? Error Code: 10022 – RHoward78 Jun 11 '15 at 18:09
  • Ah, missed that one. It translates as follows, considering .NET reference source: “The values specified for members of the socket triple are individually supported, but the given combination is not.”. It’s raised by the WSASocket function. You’ll have to debug whether it’s properly called. – Daniel B Jun 11 '15 at 18:14
  • @DanielB What would cause it to be improperly called? – RHoward78 Jun 11 '15 at 18:24
  • Most likely 10022 is the Winsock error code [WSAEINVAL](https://msdn.microsoft.com/en-us/library/aa924071.aspx). In your case all arguments are supplied by the constructor you are calling so they're unlikely to be invalid unless .NET is broken/corrupted in some way. If we assume .NET is not broken, then that suggests the WSAEINVAL is caused by the current state of the socket. (the other explanation for WSAEINVAL). All of which is a long-winded way of saying what @jonskeet said a few hours ago - there's probably something wrong either with .NET or your system's network stack – Frank Boyne Jun 11 '15 at 18:51
  • @JonSkeet I get a response when I ping 127.0.0.1. With this, I read somewhere that this indicates that my TCP/IP stack is okay. Looking into reinstalling .NET framework. Is this something that could fix the issue? – RHoward78 Jun 11 '15 at 19:00
  • @FrankBoyne see above comment as well, please sir – RHoward78 Jun 11 '15 at 19:00
  • It doesn't mean that *everything* is okay - it means that *the code required for that path is okay*. But more importantly, we're really not dealing with a programming problem here - again, this should be moved to SuperUser. – Jon Skeet Jun 11 '15 at 19:01
  • @JonSkeet yes sir. I posted to superuser. Not much feedback as of yet. Do you think reinstalling .NET could prove useful though? – RHoward78 Jun 11 '15 at 19:03

1 Answers1

3

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

Orkun Bekar
  • 1,447
  • 1
  • 15
  • 36