I'm working on a crude C# console chat application. I'm doing it to learn more about both networking and C#. So far the program works, and both the server and client are able to communicate with one another. The program works by having both the server and the client create a username, and then each waits for the other to communicate a message before being able to type in a response. My problem is that I want the server to display the message "(client's name) has connected" where (client's name) contains the actual username of the client. I try to achieve this by having the client write the name to the server, but when I implement the code, both the client and server are unresponsive. To see what I mean, run the following two programs, but uncomment the code under the sections "IS THIS LINE WRITING" and "THIS LINE DOESN'T WORK". Its a very simple application so I'm probably just missing something. Any help is greatly appreciated.
Client Code:
[CODE] using System; using System.Collections.Generic; using System.Text;
namespace BasicClientApp { using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading;
public class BasicClientApp { static public void Main(string[] Args) {
Console.WriteLine("Welcome to tugboat talk"); Console.WriteLine("Please enter your user name"); String clientName = Console.ReadLine(); Console.WriteLine("Connecting to Server..."); TcpClient theServer; NetworkStream networkStream; System.IO.StreamReader streamReader; System.IO.StreamWriter streamWriter;
try { theServer = new TcpClient(); //this is connecting to the server located at the following email address //the server should be listening on port 13000
//insert your ip address here theServer.Connect("144.249.58.10", 13000);
networkStream = theServer.GetStream(); streamReader = new System.IO.StreamReader(networkStream); streamWriter = new System.IO.StreamWriter(networkStream); Console.WriteLine("Connected to Server. Wait for message");
//******************* IS THIS LINE WRITING??? ******************************8 //streamWriter.Write(clientName); //streamWriter.Flush();
// **********************************************************************
} catch { Console.WriteLine( "Failed to connect to the server", "localhost"); Console.ReadKey(); return; }
try { string outputString = ""; string inputString = "";
try { // streamWriter.WriteLine(clientName); } catch { Console.WriteLine("Client name could not be written"); Console.ReadKey(); }
while(inputString != "quit")// read the data from the host and display it {
outputString = streamReader.ReadLine(); Console.WriteLine(outputString); inputString = Console.ReadLine(); if (inputString == "quit") break; streamWriter.WriteLine(clientName + ": " + inputString); //Console.WriteLine(inputString); streamWriter.Flush(); } } catch { Console.WriteLine("Client exited the program"); } // tidy up Console.WriteLine("You are about to exit Tugboat Talk"); Console.ReadKey(); networkStream.Close(); } } } [/CODE]
==================================== Server Application:
[CODE]
using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading;
public class BasicServerApp { public static void Main() { Int32 port = 13000; String userName; //insert your IP address here IPAddress localAddr = IPAddress.Parse("144.249.58.10"); Console.WriteLine("Tugboat Talk is starting now"); Console.WriteLine("Enter your user name "); userName = Console.ReadLine(); TcpListener tcpListener = new TcpListener(localAddr, port); tcpListener.Start(); Console.WriteLine("Waiting for a client connection..."); TcpClient client = tcpListener.AcceptTcpClient(); NetworkStream networkStream; System.IO.StreamWriter streamWriter; System.IO.StreamReader streamReader;
try{
networkStream = client.GetStream(); streamWriter = new System.IO.StreamWriter(networkStream); streamReader = new System.IO.StreamReader(networkStream); } catch { Console.WriteLine("Error opening stream. Closing application."); return; }
if (client.Connected) { Console.WriteLine("A client has connected. Please type a message");
} string theString;
//************************ THIS LINE DOESN't WORK!!!!! *****************************
//Console.WriteLine(streamReader.ReadLine() + " has connected");
//**********************************************************************************
while (client.Connected) {
try {
theString = Console.ReadLine(); streamWriter.WriteLine(userName + ": " + theString); //Console.WriteLine(theString); streamWriter.Flush(); theString = streamReader.ReadLine(); Console.WriteLine(theString); } catch { Console.WriteLine("The client disconnected."); break;
}
}
streamReader.Close(); networkStream.Close(); streamWriter.Close(); client.Close(); Console.WriteLine("Exiting..."); Console.ReadKey(); } }
[/CODE] |