954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C# socket error

Hi. I have this connection code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace ConnApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1234);
            try
            {
                sck.Connect(ip);
                Console.WriteLine("Connected!");
            }
            catch 
            {
                Console.WriteLine("Error!");
            }
            Console.Read();
        }
    }
}

It's telling me "Error!" and I cannot connect to local. But connection works on port 80. Please help me.

©lick
Newbie Poster
4 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

We need a lot more information than "it's telling me Error". What error, exactly, is it giving. What line, exactly, is the error occuring. You should catch specific exceptions, not the generic "catch" you are using. You don't even bother to get the exception object so you can see more information about the problem!

Momerath
Nearly a Senior Poster
3,384 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 

My error response, what I typed it in code - try catch.

©lick
Newbie Poster
4 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

I know, but that tells us nothing except there is an exception. Change your catch statement (and following code) to:

catch (Exception ex) {
    Console.WriteLine(ex.Message);
    if (ex.InnerException != null) {
        Console.WriteLine(ex.InnerException.Message);
    }
}
Momerath
Nearly a Senior Poster
3,384 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: