IP address

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I built two programs, one is a server and the other one is the client, I want
those two programs to work on the same machine and to exchange information
through the internet.
IPAddress ipAddress = IPAddress.Parse(my IP);
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);
I use this same code in the both programs except for the port number which
is diferent.
But there seems to be no connection.
Maybe I use the wrong Ip number, coz I don't know how to get my ip, in my
interent connections status I see two ips: Client and Server (I use ADSL)
What can be the problem?
 
Hi Michael,
I built two programs, one is a server and the other one is the client, I want
those two programs to work on the same machine and to exchange information
through the internet.
IPAddress ipAddress = IPAddress.Parse(my IP);
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);
I use this same code in the both programs except for the port number which
is diferent.
But there seems to be no connection.
Maybe I use the wrong Ip number, coz I don't know how to get my ip, in my
interent connections status I see two ips: Client and Server (I use ADSL)

Use "127.0.0.1" as an IP addresss for both programs. That's the
so called loopback (local) address.

bye
Rob
 
Michael,

Is it possible that you are behind a firewall, and that the IP address
that you are obtaining is an internal IP, not an external one?

Also, how are you getting the IP address?

I also have to ask why you are choosing to go out to the internet to
make a request to yourself on the same machine. It seems horribly
inefficient, and certainly, you could abstract some of the request/response
logic in favor of a more efficient transport (so that you can gain the
benefits of a speedier transport on the same machine, but still use the
internet if the client and server were on separate machines).

Hope this helps.
 
Well now when I tried to use 127.0.0.1 it says :
No connection could be made becase the target machine actively refused it
at System.net.sockets.socket.endconnect<IAsyncResult asyncResult>
at AsynchronousClient.ConnectCallBack<IAsyncResult ar>.....
I also disabled my firewall.
I use those two programs on the same machine just becuase I have no other
computer with another internet connection nearby, eventually my goal is to
put those programs on separated computers.
And what do you mean by internal ip and external ip?
thanks
 
hi Robert I used the 127.0.0.1 ip number but when I run the program it says:
SocketException: .....No connection could be made because the target machine
actively refused it
at System.Net.Sockets.Socket.Connect(EndPoint remooteEP)
What can be the problem?
(I disabled my Firewall)
 
I would start off by do a quick loopback test.

1) Click on Start->Run
2) Type in "cmd" and hit enter
3) Type "ping 127.0.0.1"

If the response is something like "Request timed out", then a) your machine
is blocking ICMP packets or b) there is a problem with the 127.0.0.1
address. I doubt either is the case if your firewall is disabled. Either
way, if you post those results here, I'm sure we can help you further.

ShaneB
 
I typed ping 127.0.0.1
and I got a responce:
reply from 127.0.0.1: bytes=32 time<ms ttl=128
 
Ok...then it is almost certainly a problem in the code.

Can you post your client Connect code and your server Listen code? That
will help.

ShaneB
 
The sender:

using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Text;


namespace SynSender
{
class Sender
{
[STAThread]
static void Main(string[] args)
{try{
Socket sender= new
Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 1160);



byte [] buffer= Encoding.ASCII.GetBytes("Hello<EOF>");
try
{
Console.WriteLine("Connecting");
sender.Connect(localEndPoint);
Console.WriteLine("Connected and sending");
sender.Send(buffer);
Console.WriteLine("Sent");
sender.Shutdown(SocketShutdown.Both);
}

catch (ArgumentNullException ane)
{
Console.WriteLine("ArgumentNullException : {0}",ane.ToString());
Console.Read();
}
catch (SocketException se)
{
Console.WriteLine("SocketException : {0}",se.ToString());
Console.Read();
}
catch (Exception e)
{
Console.WriteLine("Unexpected exception : {0}", e.ToString());
Console.Read();
}

}
catch (Exception e)
{
Console.WriteLine( e.ToString());
Console.Read();
}



}
}
}


The reciever:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace Reciever
{
class Recieve
{

[STAThread]
static void Main(string[] args)
{try{
Socket reciever= new
Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 1100);


Console.WriteLine("Connecting");
reciever.Bind(localEndPoint);
reciever.Listen(100);
Console.WriteLine("Waiting");
Socket handler= reciever.Accept();
Console.WriteLine("Handler accepted");
byte [] bytes=new byte[1024];
handler.Receive(bytes);
Console.WriteLine("Recieved"+bytes.ToString());

handler.Shutdown(SocketShutdown.Both);
handler.Close();
Console.WriteLine("\nPress ENTER to continue...");
Console.Read();

}

catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.Read();
}



}
}
}



But the reciever seems to work fine, it reachs the line when it shows
"waiting" on the screen, the sender is the one that causes the trouble
Thanks for your help
 
Back
Top