473,320 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Stuck with Multithreading - Please Help

I am trying to write a simple Console based Chat app. I am having
trouble multithreading, if someone can give me a hint. I am trying to
pass the NetworkStream 'ns' to the methods, but also run seperate
threads for listening and sending. Here's the code, thanks for any
input.

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
[STAThread]
public static void Main(string[] args)
{
string client, name;
TcpClient server;
Console.WriteLine("Enter Name of Server Computer:");
client = Console.ReadLine();
Console.WriteLine("Enter Your Name:");
name = Console.ReadLine();
Console.WriteLine("Searching For Server...");

try
{
server = new TcpClient(client, 45000);
}
catch (SocketException)
{
Console.WriteLine("Unable To Connect: LOCALHOST acting as
Server");
TcpListener newsock = new TcpListener(45000);
newsock.Start();
server = newsock.AcceptTcpClient();

}
NetworkStream ns = server.GetStream();
Class1 start = new Class1();
start.RunThreads();
}
public void RunThreads()
{
Thread t2 = new Thread(new ThreadStart(Send));
t2.Start();
Thread t3 = new Thread(new ThreadStart(Receive));
t3.Start();
}
public void Send()
{
while(true)
{
input = Console.ReadLine();
if (input == "exit")
break;
ns.Write(Encoding.ASCII.GetBytes(input), 0, input.Length);
ns.Flush();
}
Console.WriteLine("Disconnecting from server...");
ns.Close();
server.Close();
}

public void Receive()
{
byte[] data = new byte[1024];
int recv = ns.Read(data, 0, data.Length);
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringData);
}
}
}
Nov 15 '05 #1
0 994

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
2
by: Jenny K | last post by:
Hi everyone, I've got a console application that grabs data into 3 datatables asynchronously, and writes each datatable's data to a textfile. The problem is that the data returned by each sproc...
9
by: Popoxinhxan | last post by:
Dear experts, i want to develop an client application that consume the google search web service. In my MainForm i have a method to retrieve all the search result e.g. GetGoogleResults(). Now i...
2
by: SStory | last post by:
Here is the situation. I want to display Icons, Type of file etc from a file extension. Upon initial program load I may only need icons for certain files. But other operations will require...
2
by: shonend | last post by:
**** sorry about the length of the message. If you can't read the whole thing and still willing to help, read the last 2 paragraphs where the main problem is described. The introduction story is...
55
by: Sam | last post by:
Hi, I have a serious issue using multithreading. A sample application showing my issue can be downloaded here: http://graphicsxp.free.fr/WindowsApplication11.zip The problem is that I need to...
6
by: MeowCow | last post by:
I will try and make my question with out being too long winded. I have been doing a lot of reading on how to do multithreading and I have implemented the code from the following example on...
0
by: Need2CSharp | last post by:
Hi All, Following is a quote from an article on MSDN. Article Title: Safe, Simple Multithreading in Windows Forms URL:...
1
by: madankarmukta | last post by:
Hi All, I have the application whose runtime argument will decides how many threads I have to create.All these threads functionality may differ by a step or two.Threads are going to use the same...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.