Click here to Skip to main content
15,171,576 members
Please Sign up or sign in to vote.
3.00/5 (4 votes)
See more:
I am trying to ping an IP address from my PC but all packages are lost. I tried a ping to a website (www.google.com) and there is no problem, all packages return correctly.

I have spoken to the administrator of the address I'm trying to establish the connection with and they say everything is working OK and that my IP address is not blocked or anything.

I don't have antivirus and the firewall is off

I try to ping to IP in my LAN and it works fine

help please

[Edit - code moved here from a comment from OP]
the problem isn't in my code because my code work fine in LAN just like the ping(work fine in LAN too) but here my code TCPServer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;

namespace NetServer
{
  public delegate object ClientConnectHandler(Socket datasocket);
  public delegate void DataReceivedHandler(object obj,byte[] data);
  public delegate void ClientDisconnectHandler(object obj);

  public class TCPServer
  {
    public event ClientConnectHandler WhenClientConnect;
    public event DataReceivedHandler WhenDataReceived;
    public event ClientDisconnectHandler WhenClientDisconnect;
    Socket ConnectionSocket;
    IPEndPoint Endpoint;
    byte[] Data=new byte[10000];
    object obj=new object();
    List OnlineClients = new List();

    public TCPServer()
    {
      ConnectionSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
      Endpoint = new IPEndPoint(IPAddress.Parse(Dns.GetHostByName(Dns.GetHostName()).AddressList[0].ToString()),6666);
    }

    public TCPServer(string ip, int port)
    {
      ConnectionSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
      Endpoint = new IPEndPoint(IPAddress.Parse(ip), port);
    }

    public void Listen()
    {
      ConnectionSocket.Bind(Endpoint);
      ConnectionSocket.Listen(100);
      ConnectionSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);
    }

    public void OnClientConnect(IAsyncResult result)
    {
      Socket s = ConnectionSocket.EndAccept(result);
      OnlineClients.Add(s);
      if (WhenClientConnect != null)
        obj = WhenClientConnect(s);
      ConnectionSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);
      s.BeginReceive(Data, 0, Data.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), new SSocket { Object=obj , Socket=s});
    }

    public void OnDataReceived(IAsyncResult result)
    {
      SSocket ssocket = (SSocket)result.AsyncState;
      try
      {
        int size = ssocket.Socket.EndReceive(result);
        if (WhenDataReceived != null)
          WhenDataReceived(ssocket.Object, Data.Take(size).ToArray());
        Data = new byte[10000];
        ssocket.Socket.BeginReceive(Data, 0, Data.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), new SSocket { Object = obj, Socket = ssocket.Socket });
      }
      catch(SocketException ex)
      {
        if(ex.ErrorCode == 10054)
        {
          foreach (Socket item in OnlineClients)
            if (ssocket.Socket == item)
              OnlineClients.Remove(item);
          if (WhenClientDisconnect != null)
            WhenClientDisconnect(ssocket.Object);
        }
      }
    }
  
    public void Send(Socket socket,byte[] data)
    {
      if(socket!=null)
        socket.Send(data);
    }
  }

  public class SSocket
  {
    object obj;
    public object Object
    {
      get { return obj; }
      set { obj = value; }
    }
 
    Socket socket;

    public Socket Socket
    {
      get { return socket; }
      set { socket = value; }
    }
  }
}

TCPClient

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Windows.Forms;

namespace NetClientTest
{
  public delegate void DataReceivedHandler(byte[] data);

  public class TCPClient
  {
    byte[] data = new b

[/Edit that's all there is]
Posted
Updated 22-Feb-11 6:51am
v2
Comments
Henry Minute 22-Feb-11 10:46am
   
Unless you show the code you are using, anyone trying to answer this will only be guessing.

Use the green 'Improve question' widget to edit your question to include the code. Please don't forget to surround it with <pre> your code here </pre> tags. :)
OriginalGriff 22-Feb-11 10:47am
   
"I don't have antivirus and the firewall is off"

Do us all a favour: Install Security essentials as a minimum, and turn your firewall on.
aidin Tajadod 22-Feb-11 11:36am
   
what if you ping that address by Ping command? note that your destination may not allow ping response.
mohamedhbmaam 22-Feb-11 11:51am
   
no i have two pc when connect them locally (by switch) the ping command work fine but when i use different getway to each computer the ping command fail (time out error)

According to you words, you should add router between different gateway.
   
thank you guys very much to read and comment in my question
i found the solution and i want to shared with you
this link will explain the whole problem and the solution
http://blogs.msdn.com/b/p2p/archive/2007/07/03/ping.aspx[^]

hope this help some one
   
Comments
mohamedhbmaam 25-Feb-11 15:27pm
   
this to enable the ping in LAN but the problem still exist
finally i found the ultimate solution from a global perspective

when you say you want to ping from one computer to another the tow computer must have a global ip address (unique in the world) why?

when you request some page from your pc that request go first to your ISP (Internet Service Provider)
your ISP rewrite that request and send it for you the server (that contain the page) send response to your ISP Then the ISP send it back to you.
this process called (NAT)
that's mean you navigate throw the internet by your ISP
that's mean the server don't know your ip he just know your ISP ip
that's mean your ip is local

but the question is still exist how can i make a P2P connection?
i am still working on the answer but you can read more about PNRP
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900