Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to listen on a single IP Address?

Posted on 2008-06-19
3
Medium Priority
?
852 Views
Last Modified: 2010-05-18
Hi,

I try to write a window form, that can connect to a server (here: a web server) and send information to it.

But I have problems by receiving data from the server.
I could listen on every incoming address (port 80) but that would mean that also streams of the web browser I use at that moment would be received.

So I just want to receive data from one special address.

But I get an error:
The selected IP address is not valid in this context.

Is there any way to listen on single addresses?
(the server is my own at home and firewall settings are made)
class Tranmission
 {
  private IPAddress targetAddress;
  private IPEndPoint endpoint;
  public Tranmission()
  {
   targetAddress = IPAddress.Parse("213.211.224.32");
   endpoint = new IPEndPoint(targetAddress, 80);
  }
 
  public string listen()
  {
   string result = null;
   TcpListener listener = new TcpListener(endpoint);
   try
   {
    listener.Start();
    TcpClient client = listener.AcceptTcpClient();
    StreamReader inStream = new StreamReader(client.GetStream());
    result = inStream.ReadToEnd();
    client.Close();
    listener.Stop();
   }
   catch (SocketException ex)
   {
    MessageBox.Show(ex.ToString());
   }
   catch (StackOverflowException ex)
   {
    MessageBox.Show(ex.ToString());
   }
   return result;
  }
 
  public bool sendText(string text)
  {
   int i=0;
   do
   {
    try
    {
     TcpClient client = new TcpClient("www.gruenzeuch.de", 80);
     StreamWriter outStream = new StreamWriter(client.GetStream());
     outStream.WriteLine(text);
     outStream.Close();
     return true;
    }
    catch (SocketException ex)
    {
     i++;
    }
   }while(i<10);
   return false;
  }
 }

Open in new window

0
Comment
Question by:develc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 17

Expert Comment

by:Andres Perales
ID: 21826334
In your IIS setting you have to specifiy the IP address for that webserver to listen on...if you are using host headers this will not work...
0
 
LVL 1

Author Comment

by:develc
ID: 21826484
it is no Web-Application, it's a window form.
Yesterday I wrote a small listener, who listend on any address and it worked.
But that's not really what I want.
0
 
LVL 1

Accepted Solution

by:
develc earned 0 total points
ID: 21998459
After a long series of tries, I found the solution:


 public string listen(string targetIPAddress, int port)
  {
   string result = null;
   string[] target = targetIPAddress.Split('.');

   Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

   try
   {
    IPAddress address = IPAddress.Parse(targetIPAddress);
    IPEndPoint endpoint = new IPEndPoint(address, port);
    socket.Connect(endpoint);
    socket.Listen(1000);

    Socket clientSocket = socket.Accept();
    byte[] data = new byte[1024];

    int receivedLength = clientSocket.Receive(data);

    result = Encoding.UTF8.GetString(data);

    clientSocket.Close();
    socket.Close();

    return result;
   }
   catch(SocketException ex)
   {
    MessageBox.Show(ex.Message);
    return null;
   }
  }
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question