Client Server App TcpListener

G

Guest

I am writing a client server app which uses a desktop and a ipaq 4150 running
ppc 2003. Now i have used the msdn example code on the TcpListener Class but
when i use this example on a form the form becomes non-responsive straight
away after i click the button to start the server. Any advice on what im
doing wrong?

Here is the server code:

private void button1_Click(object sender, System.EventArgs e)
{
try
{
// Set the TcpListener on port 13000.
Int32 port = 13000;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");

// TcpListener server = new TcpListener(port);
TcpListener server = new TcpListener(localAddr, port);
// Start listening for client requests.
server.Start();

// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;

// Enter the listening loop.
while(true)
{
Console.Write("Waiting for a connection... ");

// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");
data = null;
// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();
int i;
// Loop to receive all the data sent by the client.
while((i = stream.Read(bytes, 0, bytes.Length))!=0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine(String.Format("Received: {0}", data));

// Process the data sent by the client.
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

// Send back a response.
stream.Write(msg, 0, msg.Length);
Console.WriteLine(String.Format("Sent: {0}", data));
}

// Shutdown and end connection
client.Close();
}
}
catch(SocketException exc)
{
Console.WriteLine("SocketException: {0}", exc);
}

Console.WriteLine("\nHit enter to continue...");
Console.Read();
}
 
G

Guest

fixed this problem. It was because i need a thread. But now im having
trouble connecting the client from the pda. The client connects fine off if
i run it on the same machine. But when i run it on the pda i cannot connect
to the server over lan or wifi. Any suggesstions???
 

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