4 Replies - 851 Views - Last Post: 21 July 2013 - 08:42 AM Rate Topic: -----

#1 theref  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 03-June 13

Socket can't connect to external IP

Posted 21 July 2013 - 07:23 AM

I've been fiddling around on how socket works in C#. And here's my current code

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

namespace MultiThreadServer
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpListener serverSocket = new TcpListener(IPAddress.Any, 8888);
            TcpClient clientSocket = default(TcpClient);

            int counter = 0;

            serverSocket.Start();
            Console.WriteLine(" >> " + "Server Started");

            counter = 0;
            while(true)
            {
                counter = counter + 1;
                clientSocket = serverSocket.AcceptTcpClient();
                Console.WriteLine(" >> " + "Client No:" + Convert.ToString(counter) + " started!");

                handleClient client = new handleClient();
                client.startClient(clientSocket, Convert.ToString(counter));
            }

            clientSocket.Close();
            serverSocket.Stop();
            Console.WriteLine(" >> " + "exit");
            Console.ReadLine();
        }

        public class handleClient
        {
            TcpClient clientSocket;
            string clNo;

            public void startClient(TcpClient inClientSocket, string clineNo)
            {
                this.clientSocket = inClientSocket;
                this.clNo = clineNo;
                Thread ctThread = new Thread(doChat);
                ctThread.Start();
            }

            private void doChat()
            {
                int requestCount = 0;
                byte[] bytesFrom = new byte[10025];

                string dataFromClient = null;
                Byte[] sendBytes = null;
                string serverResponse = null;
                string rCount = null;
                requestCount = 0;

                while (true)
                {
                    try
                    {
                        requestCount = requestCount + 1;
                        NetworkStream networkStream = clientSocket.GetStream();
                        networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                        dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                        dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
                        Console.WriteLine(" >> " + "From client-" + clNo + dataFromClient);

                        rCount = Convert.ToString(requestCount);
                        serverResponse = "Server to client(" + clNo + ")" + rCount;
                        sendBytes = Encoding.ASCII.GetBytes(serverResponse);
                        networkStream.Write(sendBytes, 0, sendBytes.Length);
                        networkStream.Flush();
                        Console.WriteLine(" >> " + serverResponse);
                    }
                    catch (Exception ex)
                    {
           //             Console.WriteLine(" >> " + ex.ToString());
                    }
                }
            }
        }
    }
}




Client
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;

namespace MultiThreadClient
{
    public partial class Form1 : Form
    {
        System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
        NetworkStream serverStream;


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            msg("Client Started");


            clientSocket.Connect("127.0.0.1", 8888);
            label1.Text = "Client Socket Program - Server Connected ...";

            
        }

        public void msg(string mesg)
        {
            textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + mesg;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            NetworkStream serverStream = clientSocket.GetStream();
            byte[] outStream = System.Text.Encoding.ASCII.GetBytes("Message from Client$");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();

            byte[] instream = new byte[10025];
            serverStream.Read(instream, 0, (int)clientSocket.ReceiveBufferSize);
            string returndata = System.Text.Encoding.ASCII.GetString(instream);
            msg("Data from Server : " + returndata);
        }
    }
}



Ran them both on my local PC, and it works. But then I tried to change the 127.0.0.1 into my external IP

clientSocket.Connect("127.0.0.1", 8888); -> clientSocket.Connect("36.74.179.XXX", 8888);
This one doesn't work, it said my something is actively refused it. Tested it on my PC, as well as my other pc (same network), it doesn't work. Changing it into 192.168.1.4 works well, just not my external IP.

Things that I've done
- Firewall Off
- Port forwarded already
- http://www.canyouseeme.org/ -> Port 8888 (Success: I can see your service on 36.74.179.XXX on port (8888))

Posted Image

Weird thing is, the server responded when I made port check request from CanYouSeeMe.org, but it just doesn't work with my Client request. :whatsthat:/> can anyone tell me if I missed something?
Thanks!

This post has been edited by theref: 21 July 2013 - 07:25 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Socket can't connect to external IP

#2 AVReidy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Re: Socket can't connect to external IP

Posted 21 July 2013 - 07:59 AM

Have you tried something like Connect("google.com", 80);? If that doesn't work, try pinging the same address from your command line.
Was This Post Helpful? 0
  • +
  • -

#3 theref  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 03-June 13

Re: Socket can't connect to external IP

Posted 21 July 2013 - 08:16 AM

Ok, changed to clientSocket.Connect("google.com", 80);
It works good, the label1 text changed into "Client Socket Program - Server Connected ...", which previously stayed as label1 text when using my external IP.

Here is the ping to my external IP
Posted Image

No packet loss,

I tried changing the port
clientSocket.Connect("36.74.179.XXX", 8888); -> into -> clientSocket.Connect("36.74.179.XXX", 80);
This time no error message when I pressed the button1, and this is what I got
Posted Image

Maybe there's something wrong with my port 8888?? But CanYouSeeMe can access it properly when my server.exe is running, and can't access it when my server.exe is off.
Was This Post Helpful? 0
  • +
  • -

#4 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,524
  • Joined: 08-April 09

Re: Socket can't connect to external IP

Posted 21 July 2013 - 08:39 AM

you are getting 400 bad request error

The 4xx class of status code is intended for cases in which the client seems to have erred. Except when responding to a HEAD request, the server SHOULD include an entity containing an explanation of the error situation, and whether it is a temporary or permanent condition. These status codes are applicable to any request method. User agents SHOULD display any included entity to the user.

If the client is sending data, a server implementation using TCP SHOULD be careful to ensure that the client acknowledges receipt of the packet(s) containing the response, before the server closes the input connection. If the client continues sending data to the server after the close, the server's TCP stack will send a reset packet to the client, which may erase the client's unacknowledged input buffers before they can be read and interpreted by the HTTP application.

400 Bad Request

The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.

From W3.org

This post has been edited by Michael26: 21 July 2013 - 08:40 AM

Was This Post Helpful? 0
  • +
  • -

#5 theref  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 03-June 13

Re: Socket can't connect to external IP

Posted 21 July 2013 - 08:42 AM

Ok so there is nothing wrong with the code.

I sent the client to my friend outside my network, and it works fine as it should be. But in my local network, it seems I just can't use external IP in this and only local ip would work, is it because of my modem?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1