Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
.NET
.NET Core
.NET MAUI
.NET Standard
Active Directory
ADO.NET
Agile Development
AI
AJAX
AlbertAGPT
Alchemy
Alexa Skills
Algorand
Algorithms in C#
Android
Angular
ArcObject
ASP.NET
ASP.NET Core
Augmented Reality
Avalanche
AWS
Azure
Backbonejs
Big Data
BizTalk Server
Blazor
Blockchain
Bootstrap
Bot Framework
Business
Business Intelligence(BI)
C#
C# Corner
C# Strings
C, C++, MFC
Career Advice
Careers and Jobs
Chapters
ChatGPT
Cloud
Coding Best Practices
Cognitive Services
COM Interop
Compact Framework
Copilot
Cortana Development
Cosmos DB
Cryptocurrency
Cryptography
Crystal Reports
CSS
Current Affairs
Custom Controls
Cyber Security
Data Mining
Data Science
Databases & DBA
Databricks
Design Patterns & Practices
DevExpress
DevOps
DirectX
Dynamics CRM
Enterprise Development
Entity Framework
Error Zone
Exception Handling
F#
Files, Directory, IO
Flutter
Games Programming
GDI+
General
Generative AI
GO
Google Cloud
Google Development
Graphics Design
Graphite Studio
Hardware
Hiring and Recruitment
HoloLens
How do I
HTML 5
Infragistics
Internet & Web
Internet of Things
Ionic
Java
Java and .NET
JavaScript
JQuery
JSON
JSP
Knockout
Kotlin
Langchain
Leadership
Learn .NET
Learn iOS Programming
LINQ
Machine Learning
Metaverse
Microsoft 365
Microsoft Fabric
Microsoft Office
Microsoft Phone
Microsoft Teams
Mobile Development
MongoDB
MuleSoft
MySQL
NEAR
NetBeans
Networking
NFT
NoCode LowCode
Node.js
Office Development
OOP/OOD
Open Source
Operating Systems
Oracle
Outsourcing
Philosophy
PHP
Polygon
PostgreSQL
Power Apps
Power Automate
Power BI
Power Pages
Printing in C#
Products
Progress
Progressive Web Apps
Project Management
Public Speaking
Python
Q#
QlikView
Quantum Computing
R
React
React Native
Reports using C#
Robotics & Hardware
RPA
Ruby on Rails
RUST
Salesforce
Security
Servers
ServiceNow
SharePoint
Sharp Economy
SignalR
Smart Devices
Snowflake
Software Architecture/Engineering
Software Testing
Solana
Solidity
Sports
SQL
SQL Server
Startups
Stratis Blockchain
Swift
SyncFusion
Threading
Tools
TypeScript
Unity
UWP
Visual Basic .NET
Visual Studio
Vue.js
WCF
Wearables
Web API
Web Design
Web Development
Web3
Windows
Windows Controls
Windows Forms
Windows PowerShell
Windows Services
Workflow Foundation
WPF
Xamarin
XAML
XML
XNA
XSharp
Register
Login
1
Answer
C# Networking Problem
Brendan
16y
2.7k
1
Reply
C# networking problem
Posted:
02-12-2009 11:04 AM
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]
Post
Reset
Cancel
Answers (
1
)
Next Recommended Forum
Problem in Reading Binary File
Advice on Exception Handling.