473,394 Members | 1,706 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,394 software developers and data experts.

Network Chat Implimentation

I'm having trouble with a network chat based server I'm working on in
C#, It works find during the reads, but as soon as I start sending back
data my reads from that point on are corrupted. If there's any advice,
it'd be greatly appreciated :)

// created on 11/07/2004 at 19:26

using System;
using System.Net.Sockets;

class ClientTCP {

private Socket socket;
private NetworkStream networkStream;
private byte[] buffer;
private AsyncCallback callbackRead;
private AsyncCallback callbackWrite;
private static int numofSockets = 0;
private int SocketNumber;
private Player player;
public ClientTCP(Socket socketForClient, int ObjectNumber)
{
socket = socketForClient;
networkStream = new NetworkStream(socket);
buffer = new byte[256];
callbackRead = new AsyncCallback(this.OnReadComplete);
callbackWrite = new AsyncCallback(this.OnWriteComplete);

SocketNumber = ObjectNumber;
ObjectStores.clienttcp[ObjectNumber] = this;
ObjectStores.player[ObjectNumber] = player = new Player(this,
ObjectNumber);

Console.WriteLine("TCP Connection!");
ReadStream();
}

private void OnReadComplete(IAsyncResult ar)
{
int bytesRead = networkStream.EndRead(ar);
String s = System.Text.Encoding.ASCII.GetString (buffer, 0, bytesRead);
Console.WriteLine("Socket #{1}: Bytes Read: {0}\nData: {2}",
bytesRead, SocketNumber, s);
player.Recieve(s);
ReadStream();
}

private void OnWriteComplete( IAsyncResult ar )
{
networkStream.EndWrite(ar);
Array.Clear(buffer, 0, buffer.Length);
ReadStream();
}

public void Send(string s)
{
s += "\n\n";
buffer = System.Text.Encoding.ASCII.GetBytes(s);

networkStream.BeginWrite(buffer, 0, s.Length, callbackWrite, null);
}

private void ReadStream()
{
networkStream.BeginRead (buffer, 0, buffer.Length, callbackRead, null);
}
}
Nov 16 '05 #1
0 1300

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

Similar topics

2
by: Jos Yule | last post by:
Hello all. I doing a project which uses Macromedia's Flash program and Python. This is a 'because i want to see if i can do it' kind of thing, so i know that there will be better/easier ways to...
10
by: sidewinder | last post by:
If it does, and it is going between a little endian and a big endian machines can we byte swap it as an int or long? Or does the mantissa or exponent cross 2 byte boundarys? Can I just call...
11
by: Parahat Melayev | last post by:
Hi I am programming a multi-threaded asyncronous server with .NET But when systems thread count reaches to 1350 system can't respond. Simply my design is creating a Reading Thread after...
0
by: Jaspreet | last post by:
Hi All, I want to make a Chat Server and Client program in C++ using Turbo/Borland C++ on a windows/Dos platform. if you have any info, suggestions or resources for Network programming in...
3
by: A | last post by:
Hi all! I am currently working on a project where I need to create a web-based chat application that will be used for auctions. If possible, could I get some advice on how to create chat...
10
by: Woody Splawn | last post by:
I have been developing a ClientServer application on one machine at my office but the time has come to transfer it to the customer. The customer is running a Windows 2000 local area network. I...
0
The1corrupted
by: The1corrupted | last post by:
Alright, I've set up a chat system that only works half way... I need suggestions on how to make it work better... I'm trying to figure out a way to make $_SESSION viewable to all players by...
14
cjbrx3115
by: cjbrx3115 | last post by:
Hey guys, I was looking around the internet a bit but couldn't find anything on my subject. I want to make an application that will be able to chat with another computer (over a network), in VB 2005...
1
oll3i
by: oll3i | last post by:
how do i run this example ? with openjms package com.ociweb.jms; import java.io.*; import javax.jms.*; import javax.naming.*;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.