Chat server/cleint

07/29/2009 12:39 Santa#1
Ok can anyone help me with creating a Chat Server and Chat Client. And please dont close this because im trying to make one so that i can put the client in the CO folder so people can talk to one another. Now if your just gonna say "Google it" or "Search" do not post, or i will report you. I have tried google. And none of them make sense to me, or even work. Any help would be appreciated:mofo:
07/29/2009 15:08 xellios#2
[Chat client]
Code:
/*
CREDITS TO SIDZONE
*/
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class TcpClientSample
{
  
    public static void Main()
   {
      byte[] data = new byte[1024];
      string input;
      int port;
      TcpClient server;
      
      System.Console.WriteLine ("Please Enter the port number of Server:\n");
      port = Int32.Parse( System.Console.ReadLine());
      try
      {
         server = new TcpClient("127.0.0.1",port);
      } catch (SocketException)
      {
         Console.WriteLine("Unable to connect to server");
         return;
      }
      Console.WriteLine("Connected to the Server...");
      Console.WriteLine("Enter the message to send it to the Sever");
        NetworkStream ns = server.GetStream();

      StateObject state = new StateObject();
      state.workSocket = server.Client;
      server.Client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                  new AsyncCallback((new TcpClientSample()).OnReceive), state);

      while(true)
      {
         input = Console.ReadLine();
         if (input == "exit")
            break;
         ns.Write(Encoding.ASCII.GetBytes(input), 0, input.Length);
         ns.Flush();

         //data = new byte[1024];
         //recv = ns.Read(data, 0, data.Length);
         //stringData = Encoding.ASCII.GetString(data, 0, recv);
         //Console.WriteLine(stringData);
      }
      Console.WriteLine("Disconnecting from server...");
      ns.Close();
      server.Close();
   }


    public void OnReceive(IAsyncResult ar)
    {
        String content = String.Empty;

        // Retrieve the state object and the handler socket
        // from the asynchronous state object.
        StateObject state = (StateObject)ar.AsyncState;
        Socket handler = state.workSocket;
        int bytesRead;

        if (handler.Connected) // Credits to cidzone
        {

            // Read data from the client socket. 
            try
            {
                bytesRead = handler.EndReceive(ar);
                if (bytesRead > 0)
                {
                    // There  might be more data, so store the data received so far.
                    state.sb.Remove(0, state.sb.Length);
                    state.sb.Append(Encoding.ASCII.GetString(
                                     state.buffer, 0, bytesRead));

                    // Display Text in Rich Text Box
                    content = state.sb.ToString();
                    Console.WriteLine(content);

                    handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                        new AsyncCallback(OnReceive), state);

                }
            }

            catch (SocketException socketException)
            {
                //WSAECONNRESET, the other side closed impolitely
                if (socketException.ErrorCode == 10054 || ((socketException.ErrorCode != 10004) && (socketException.ErrorCode != 10053)))
                {
                    handler.Close();
                }
            }

        // Eat up exception....Meh
            catch (Exception exception)
            {
                //MessageBox.Show(exception.Message + "\n" + exception.StackTrace);

            }
        }
    }


}

public class StateObject
{
    // Client  socket.
    public Socket workSocket = null;
    // Size of receive buffer.
    public const int BufferSize = 1024;
    // Receive buffer.
    public byte[] buffer = new byte[BufferSize];
    // Received data string.
    public StringBuilder sb = new StringBuilder();
}


-----------------------------------
Chat Server

[Only registered and activated users can see links. Click Here To Register...]

-----------------------------------

Add credits in your use.
+Thanks if u like it



#Request to close threadstarter got his request^^
07/29/2009 16:39 _Emme_#3
Quote:
Originally Posted by xellios View Post
[Chat client]

-----------------------------------
Chat Server

[Only registered and activated users can see links. Click Here To Register...]

-----------------------------------

Add credits in your use.
+Thanks if u like it



#Request to close threadstarter got his request^^
Yeah, give credits to Sidzone..
xellios, don't take other codes and claim them as your own.

[Only registered and activated users can see links. Click Here To Register...]
07/29/2009 16:44 f0am#4
or you can use this too [Only registered and activated users can see links. Click Here To Register...]
07/29/2009 21:54 Santa#5
Quote:
Originally Posted by xellios View Post
[Chat client]
Code:
/*
CREDITS TO SIDZONE
*/
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class TcpClientSample
{
  
    public static void Main()
   {
      byte[] data = new byte[1024];
      string input;
      int port;
      TcpClient server;
      
      System.Console.WriteLine ("Please Enter the port number of Server:\n");
      port = Int32.Parse( System.Console.ReadLine());
      try
      {
         server = new TcpClient("127.0.0.1",port);
      } catch (SocketException)
      {
         Console.WriteLine("Unable to connect to server");
         return;
      }
      Console.WriteLine("Connected to the Server...");
      Console.WriteLine("Enter the message to send it to the Sever");
        NetworkStream ns = server.GetStream();

      StateObject state = new StateObject();
      state.workSocket = server.Client;
      server.Client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                  new AsyncCallback((new TcpClientSample()).OnReceive), state);

      while(true)
      {
         input = Console.ReadLine();
         if (input == "exit")
            break;
         ns.Write(Encoding.ASCII.GetBytes(input), 0, input.Length);
         ns.Flush();

         //data = new byte[1024];
         //recv = ns.Read(data, 0, data.Length);
         //stringData = Encoding.ASCII.GetString(data, 0, recv);
         //Console.WriteLine(stringData);
      }
      Console.WriteLine("Disconnecting from server...");
      ns.Close();
      server.Close();
   }


    public void OnReceive(IAsyncResult ar)
    {
        String content = String.Empty;

        // Retrieve the state object and the handler socket
        // from the asynchronous state object.
        StateObject state = (StateObject)ar.AsyncState;
        Socket handler = state.workSocket;
        int bytesRead;

        if (handler.Connected) // Credits to cidzone
        {

            // Read data from the client socket. 
            try
            {
                bytesRead = handler.EndReceive(ar);
                if (bytesRead > 0)
                {
                    // There  might be more data, so store the data received so far.
                    state.sb.Remove(0, state.sb.Length);
                    state.sb.Append(Encoding.ASCII.GetString(
                                     state.buffer, 0, bytesRead));

                    // Display Text in Rich Text Box
                    content = state.sb.ToString();
                    Console.WriteLine(content);

                    handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                        new AsyncCallback(OnReceive), state);

                }
            }

            catch (SocketException socketException)
            {
                //WSAECONNRESET, the other side closed impolitely
                if (socketException.ErrorCode == 10054 || ((socketException.ErrorCode != 10004) && (socketException.ErrorCode != 10053)))
                {
                    handler.Close();
                }
            }

        // Eat up exception....Meh
            catch (Exception exception)
            {
                //MessageBox.Show(exception.Message + "\n" + exception.StackTrace);

            }
        }
    }


}

public class StateObject
{
    // Client  socket.
    public Socket workSocket = null;
    // Size of receive buffer.
    public const int BufferSize = 1024;
    // Receive buffer.
    public byte[] buffer = new byte[BufferSize];
    // Received data string.
    public StringBuilder sb = new StringBuilder();
}


-----------------------------------
Chat Server

[Only registered and activated users can see links. Click Here To Register...]

-----------------------------------

Add credits in your use.
+Thanks if u like it



#Request to close threadstarter got his request^^
Thanks, hope it works, notice there is no form in there? Any idea on how to add one to work?

Quote:
Originally Posted by _StarScream_ View Post
Google it, or search it -.-

:P
xD, couldn't help but laugh cause the little face

Quote:
Originally Posted by xellios View Post
Why do you even respond to it i gave him a answer there is no need being "funny" ffs?
I found it funny....

Quote:
Originally Posted by EmmeTheCoder View Post
Yeah, give credits to Sidzone..
xellios, don't take other codes and claim them as your own.

[Only registered and activated users can see links. Click Here To Register...]
I knew the credits were to sidzone, as its in the code...

Quote:
Originally Posted by f0am View Post
or you can use this too [Only registered and activated users can see links. Click Here To Register...]
Aha, hope i can get it to work xD
07/30/2009 05:07 kinshi88#6
#Cleaned

Anymore arguing and flaming will result in infractions for all of you.
07/30/2009 05:09 Santa#7
Quote:
Originally Posted by kinshi88 View Post
#Cleaned

Anymore arguing and flaming will result in infractions for all of you.
Emme too?