[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 605
  • Last Modified:

Server/Client not connecting if NOT localhost...

Client:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace client
{
    class Program
    {
        static TcpClient client;

        static void Main(string[] args)
        {
            Console.Title = "Client";

            client = new TcpClient();
            client.Connect(new IPEndPoint(IPAddress.Parse("79.177.221.185"), 13000));

            while (client.Connected)
            {
            sendevent:
                // send();
                receive();
                goto recivenet;
            recivenet:
                //  receive();
                send();
                goto sendevent;
            }
        }

        static byte[] buffer = new byte[4096];

        static void receive()
        {
            NetworkStream read = client.GetStream();
            int data = read.Read(buffer, 0, 4096);
            Console.WriteLine("Server response: " + Encoding.ASCII.GetString(buffer, 0, data));
           // read.Close();
        }

        static void send()
        {
            byte[] data = Encoding.ASCII.GetBytes(Console.ReadLine());
            NetworkStream stream = client.GetStream();
            stream.Write(data, 0, data.Length);
           // stream.Dispose();
        }
    }
}

Open in new window



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

namespace Server
{
    class Program
    {
        static TcpClient connected;

        static void Main(string[] args)
        {
            Console.Title = "Server";

            TcpListener listener = new TcpListener(IPAddress.Any, 13000);
            listener.Start();

            while (true)
            {
                Console.WriteLine("Waiting for a connection... ");

                connected = listener.AcceptTcpClient();
                Console.WriteLine("Connected!.");

            sendevent:
                send();
                goto recivenet;
            recivenet:
                receive();
                goto sendevent;
            }


        }
        static byte[] buffer = new byte[4096];

        static void receive()
        {
            NetworkStream read = connected.GetStream();
            int data = read.Read(buffer, 0, 4096);
            Console.WriteLine("Client response: " + Encoding.ASCII.GetString(buffer, 0, data));
           // read.Close();
        }

        static void send()
        {
            byte[] data = Encoding.ASCII.GetBytes(Console.ReadLine());
            NetworkStream stream = connected.GetStream();
            stream.Write(data, 0, data.Length);
           // stream.Dispose();
        }
    }
}

Open in new window



I have problem accepting connections from other PCs... if i enter 127.0.0.1 and running it on my same PC, it works... but if i enter my WAN IP others get an exception and are unable to connect.
0
rotem156
Asked:
rotem156
1 Solution
 
vinhnlCommented:
Have you tried to turn off firewall ? Computers are on same LAN ? Can they ping each other ? Could you try to ping the server with this port from the client ?
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now