TCP / IP connection problem

  • Thread starter Thread starter gaggs1
  • Start date Start date
G

gaggs1

Guest
I have a code that communicate with android application via TCP/IP protocal my code works on a fix IP (on my machine) now I want to make it mobile (work on all machines) then I want to get Server IP (local machine IP) but when i do that the code show me an Exception.

the Exception:

System.FormatException : 'Une adresse IP non valide a été spécifiée.'

Mycode:

static string send="",name1 = "", name2 = "", name3 = "", prix1 = "", prix2 = "", prix3 = "";

const int PORT_NO = 7800;

static string SERVER_IP ="";
char[] data=null;
static void Main(string[] args)
{

while (true)
{
//SERVER_IP = GetIP();
Console.WriteLine(Dns.GetHostName());
IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
foreach (IPAddress addr in localIPs)
{
if (addr.AddressFamily == AddressFamily.InterNetwork)
{

Console.WriteLine(addr);
}
}
Console.WriteLine(SERVER_IP);
SERVER_IP = Dns.GetHostName().ToString();
string m = " * ";
var xx = new List<string>();
xx.Clear();
Console.ReadLine();
//---listen at the specified IP and port no.---
IPAddress localAdd = IPAddress.Parse(SERVER_IP);
TcpListener listener = new TcpListener(localAdd, PORT_NO);
Console.WriteLine("Listening...");
listener.Start();

//---incoming client connected---
TcpClient client = listener.AcceptTcpClient();
Console.WriteLine("Connect...");
//---get the incoming data through a network stream---

NetworkStream nwStream = client.GetStream();
byte[] buffer = new byte[client.ReceiveBufferSize];

//---read incoming stream---
int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize);

//---convert the data received into a string---
string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead);
if (dataReceived != "")
{
var charsToRemove = new string[] { "{", "}", "Uii", "0=", ":", " " };
foreach (var ca in charsToRemove)
{
dataReceived = dataReceived.Replace(ca, string.Empty);
}
dataReceived = dataReceived.Substring(4);
string dataReceived1 = dataReceived.Substring(0, 24);
Console.WriteLine("Received : " + dataReceived);
Console.WriteLine("dataLength : " + dataReceived.Length);


var charsToRemove1 = new string[] { " " };
foreach (var ca in charsToRemove1)
{
dataReceived = dataReceived.Replace(ca, string.Empty);
}
if (dataReceived.Length < 30)
{
name1 = chargeN(dataReceived);
prix1 = chargeP(dataReceived);
m = m + "#" + name1 + "*" + prix1;
}
else
{

while (dataReceived.Length > 48)
{
xx.Add(dataReceived.Substring(0, 24));
dataReceived = dataReceived.Substring(28);

}
dataReceived = dataReceived.Substring(0, 24);
xx.Add(dataReceived);
int x = xx.Count;
Console.WriteLine("LenghtArray : " + x);
for (int i = 0; i < xx.Count; i++)
{
Console.WriteLine("Array ele : " + xx);
name1 = chargeN(xx);
prix1 = chargeP(xx);
m = m + "#" + name1+"*"+prix1;
}
Console.WriteLine("Full array : " + m);


}

//---write back the text to the client---
//nwStream.Write(buffer, 0, bytesRead);
byte[] msg = System.Text.Encoding.ASCII.GetBytes(m);
Console.WriteLine("Sending back : " + m);
// Send back a response.
nwStream.Write(msg, 0, msg.Length);

}

client.Close();
listener.Stop();
}

}


Plz help me to fix it

Continue reading...
 
Back
Top