Dito Dito

Dito Dito

  • NA
  • 3
  • 3.2k

Chat-like TCP communication problem

Sep 6 2010 4:09 PM
Wel... Ive been working on a chat application for a week now and Im desperate right now...

the thing is that my application works on LAN but it doesnt work through internet. When i use my local IP the connection is made but when I get my IP from whatsmyip.com and use it there are numerous problems...

here's the server:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace TCPChatServer
{
class Program
{
public static TcpListener tcpListener;
public static int port;
public static IPAddress ip;
public static TcpClient tcpClient;


static void Main()
{
GetMyIP.get();
ip = GetMyIP.ip;
ip = IPAddress.Parse("93.139.3.97");
port = 15555;
tcpListener = new TcpListener(IPAddress.Any,port);

// so. when i use IPAddress.Any it doesnt get through and when i use my ip i get error: The requested address is not valid in its context
// and when i use my local ip (192.168.1.4) everything works fine... im out of ideas...


tcpListener.Start();

tcpClient = tcpListener.AcceptTcpClient();

NetworkStream netStream = tcpClient.GetStream();
Byte[] message = new Byte[1000];
int bytesRead;

bytesRead = netStream.Read(message, 0, 100);

string sms = ASCIIEncoding.ASCII.GetString(message);
Console.WriteLine(sms);

Console.WriteLine("end");
Console.Read();

}
}
}

and here's the "client":
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace SimpleChatClient
{
class Program
{

static void Main()
{
int trace = 0;

TcpClient client = new TcpClient();

// 93.139.3.97 192.168.1.4
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("93.139.3.97"), 15555);
Console.WriteLine(trace++);
client.Connect(IPAddress.Parse("93.139.3.97"), 15555);

Console.WriteLine(trace++);
NetworkStream clientStream = client.GetStream();

Console.WriteLine(trace++);
ASCIIEncoding encoder = new ASCIIEncoding();
byte[] buffer = encoder.GetBytes("Hello Server!");

Console.WriteLine(trace++);
clientStream.Write(buffer, 0, buffer.Length);
Console.WriteLine(trace++);

clientStream.Flush();


}
}
}





Answers (3)