Jump to content

TCP Socket Connection Problem

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Daemon

Daemon

    Newbie

  • Members
  • Pip
  • 3 posts
Hey Dudes,
my first post so pls be kind :).
Started few days ago with C# and .NET using Visual Studio C# Express 2008.
In school we are coding in Java and started socket-programming but i wanted to do something new and fresh so decided to take a look at C#.
And having a try on a simple chat using TCP sockets.
Now about my Problem:
I don't know why my program doesn't connect to itself at another PC in my House's LAN, could be about my PCs (Firewall etc.) but i want to elimate the possibility of a programming mistake.
However It DOES connect and i can chat when i start 2 instances on the same PC and parameters are "127.0.0.1" and e.g. "5221".
Feel free to ask and thanks in advance.
Here's the relevant Code:

static Socket Socket;


internal static String connect(string sIP, string sPort)

        {

            try

            {

                IPAddress IP = IPAddress.Parse(sIP);

                Int32 Port = Convert.ToInt32(sPort);

                IPEndPoint Endpoint = new IPEndPoint(IP, Port);

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

                Socket.Connect(Endpoint);

            }

            catch (Exception e)

            {

                throw e;

            }

            return "Status: Connected!";

        }


internal static String listen(string sIP, string sPort)

        {

            try

            {

                IPAddress IP = IPAddress.Parse(sIP);

                Int32 Port = Convert.ToInt32(sPort);

                IPEndPoint Endpoint = new IPEndPoint(IP, Port);

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

                ListeningSocket.Bind(Endpoint);

                ListeningSocket.Listen(1);

                Socket = ListeningSocket.Accept();

                ListeningSocket.Close();

            }

            catch (Exception e)

            {

                throw e;

            }

            return "Status: Connected!";

        }


Edited by Daemon, 13 March 2010 - 05:02 PM.


#2
abdul.gafur

abdul.gafur

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
I've never tried to create server client application, using C# and .net. Only using C, console application.

Hope the following links help
Page 2 - Socket Programming in C# - Part I
Socket Programming in C#

#3
Daemon

Daemon

    Newbie

  • Members
  • Pip
  • 3 posts
Thx for reply, but i'm really stuck on this.
i read several tutorials and the msdn-library and i really think it should work and it does work on the same machine. I just need someone who's in this socket programming with c#.

Edited by Daemon, 14 March 2010 - 07:34 AM.