The requested address is not valid in its context

R

ryu

Hi, I have written a sample program that acts as a simple server that prints
out what the client sends to it. It works well when I run both client and
server on the same machine. However when they are seperate machines, I
encountered the following "The requested address is not valid in its
context."

The following is the sample program

------------------------------------Client------------------------------------
public Form1()

{

InitializeComponent();

try

{

InitializeComponent();

IPAddress ip = IPAddress.Parse("127.0.0.1");

IPEndPoint ep = new IPEndPoint(ip, 65000);

// Console.WriteLine( "Connecting to {0}", serverName );

TcpClient tcpSocket = new TcpClient( ep );

// TcpClient tcpSocket = new TcpClient( serverName, 65000 );

streamToServer = tcpSocket.GetStream();

}

catch(SocketException err)

{

MessageBox.Show(err.Message);

}

}



private void button1_Click(object sender, System.EventArgs e)

{

string message = txtMsg.Text.Trim();

Console.WriteLine(

"Sending {0} to server.", message );

// create a streamWriter and use it to

// write a string to the server

System.IO.StreamWriter writer =

new System.IO.StreamWriter( streamToServer );

writer.WriteLine( message );

writer.Flush();

// Read response

System.IO.StreamReader reader =

new System.IO.StreamReader( streamToServer );

string strResponse = reader.ReadLine();

Console.WriteLine( "Received: {0}", strResponse );

// streamToServer.Close();

}

------------------------------------Server------------------------------------

private void Run()

{

// create a new TcpListener and start it up

// listening on port 65000

// IPAddress localAddr = IPAddress.Parse( "127.0.0.1" );

IPAddress localAddr = Dns.Resolve(Dns.GetHostName()).AddressList[0];

Console.WriteLine( localAddr.ToString() );

TcpListener tcpListener = new TcpListener( localAddr, 65000 );

tcpListener.Start();

Console.WriteLine( "Listening..." );

// keep listening until you send the file

for ( ; ; )

{

// if a client connects, accept the connection

// and return a new socket named socketForClient

// while tcpListener keeps listening

Socket socketForClient =

tcpListener.AcceptSocket();

Console.WriteLine( "Client connected" );

ClientHandler handler =

new ClientHandler( socketForClient );

handler.StartRead();

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top