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
/*
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();
}
/*
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
-----------------------------------
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_
Google it, or search it -.-
:P
xD, couldn't help but laugh cause the little face
Quote:
Originally Posted by xellios
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
Yeah, give credits to Sidzone..
xellios, don't take other codes and claim them as your own.
I knew the credits were to sidzone, as its in the code...
Exo Metin2 P-server cleint/exe 07/30/2010 - Metin2 Private Server - 7 Replies Huhu
ich spiele seit heute aufm p-server
Exo Metin2.
aber da sehen die fertigkeiten voll krank
aus und ich wollte fragen ob einer vlt ne exe oder nen clienten hat
damit des normal aussieht
P-Server CHAT und GM CHAT /b /n Problem HILFE 11/07/2009 - Metin2 Private Server - 5 Replies Hallo leute,
Also, seit dem ich Updates auf meinem PC insterlliert habe und den PC neugestartet habe, hat mein Server irgendein Problem bekommen. Wenn ich mich einlogge kann ich noch Items erstellen kämpfen usw.. außer in irgendeiner Form Chatten. So zB. wenn ich versuche was zu schreiben kommt der Satz halt nicht über dem Kopf oder im Chat und dann ist alles weg also kann keine Items erstellen und nur Attacken einsetzten MObs kann ich nicht töten, also so als ob das Internet dann weg wäre....
NO-DC CLEINT PLS:) 04/06/2009 - SRO Private Server - 1 Replies guys where i can get a no-dc client for p server??