chatting program

  • Thread starter Thread starter Britney
  • Start date Start date
B

Britney

hi guys,
how hard is it to develop a small chatting program?
is there an example out there? I don't want to have a web browser based
application, but window application. what libraries do I need to use to
develop it?
 
how hard is it to develop a small chatting program?
is there an example out there? I don't want to have a web browser based
application, but window application. what libraries do I need to use to
develop it?

Take a look at using sockets and TCP/IP and just send strings from one
socket to another.

Joanna
 
But can you tell me how hard is it?
from 1 to 10, 1 is easiest, 10 is hardest, what do you think ? how long
will it take me to learn sockets and TCP/IP programming? and do you have
some good website about sockets and TCP/IP programming?
 
It really all depends, how much exprience do you have, how fast do you
learn, how much ready-to-use code you can find.

Just try it out, it's better than whatever anybody tells you.
 
well... can someone provide me an example to create chatting program? or
tell me how does chatting work with TCP/IP and socket? I don't know how
the process go. To my understanding, socket is like a tunnel that connects
one computer to the other computer, a socket is defined by IP address+port
number. that's all I know.
 
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class SimpleTcpSrvr
{
public static void Main()
{
int recv;
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Any,
9050);
Socket newsock = new
Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
newsock.Bind(ipep);
newsock.Listen(10);
Console.WriteLine("Waiting for a client...");
Socket client = newsock.Accept();
IPEndPoint clientep =
(IPEndPoint)client.RemoteEndPoint;
Console.WriteLine("Connected with {0} at port {1}",
clientep.Address, clientep.Port);

string welcome = "Welcome to my test server";
data = Encoding.ASCII.GetBytes(welcome);
client.Send(data, data.Length,
SocketFlags.None);
while(true)
{
data = new byte[1024];
recv = client.Receive(data);
if (recv == 0)
break;

Console.WriteLine(
Encoding.ASCII.GetString(data, 0, recv));
client.Send(data, recv, SocketFlags.None);
}
Console.WriteLine("Disconnected from {0}",
clientep.Address);
client.Close();
newsock.Close();
}
}class SimpleTcpClient
{
public static void Main()
{
byte[] data = new byte[1024];
string input, stringData;
IPEndPoint ipep = new IPEndPoint(
IPAddress.Parse("127.0.0.1"), 9050);
Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
try
{
server.Connect(ipep);
} catch (SocketException e)
{
Console.WriteLine("Unable to connect to server.");
Console.WriteLine(e.ToString());
return;
}
int recv = server.Receive(data);
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringData);
while(true)
{
input = Console.ReadLine();
if (input == "exit")
break;
server.Send(Encoding.ASCII.GetBytes(input));
data = new byte[1024];
recv = server.Receive(data);
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringData);
}
Console.WriteLine("Disconnecting from server...");
server.Shutdown(SocketShutdown.Both);
server.Close();
}
}Lifted from "C# Network Programming" by Richard Blum. If it's useful to
you, consider buying the book.-- Regards,Tim
HaughtonAgitekhttp://agitek.co.ukhttp://blogitek.com/timhaughton
 
Tim,
I got this error when I run it. any idea why?
I opened port 9050 in the firewall, but I still got this error.


Unable to connect to server.
System.Net.Sockets.SocketException: No connection could be made because the
targ
et machine actively refused it
at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
at SimpleTcpClient.Main() in c:\documents and settings\brit\m
y documents\visual studio projects\consoleapplication5\class1.cs:line 60
Press any key to continue
 
..net remoting is also a viable issue. It allows you to pass objects and
such without worrying about the serialization issue that sockets create
 
Britney said:
Tim,
I got this error when I run it. any idea why?
I opened port 9050 in the firewall, but I still got this error.


Unable to connect to server.
System.Net.Sockets.SocketException: No connection could be made because
the targ
et machine actively refused it
at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
at SimpleTcpClient.Main() in c:\documents and settings\brit\m
y documents\visual studio projects\consoleapplication5\class1.cs:line 60
Press any key to continue
<snip>

Have you set the SocketPermissions? If you don't, this will prevent you from
connecting. (on Windows 2000 at least).

HTH
Ron.
 
i'm using win xp sp2
how do I set permission?


Ron said:
<snip>

Have you set the SocketPermissions? If you don't, this will prevent you
from connecting. (on Windows 2000 at least).

HTH
Ron.
 
Britney said:
i'm using win xp sp2
how do I set permission?
<snip>
Look at the MSDN/Visual Studio .NET Sockets help. There are examples of a
simple Socket Client and Server, and Socket Permissions. That should give
you all you need to know about Sockets.

Ron.
 
Britney -

Most likely your connection is getting blocked by the XP SP2
firewall feature. You can check that by looking at your network
connection properties, and clicking the Advanced tab. Just list port
9050 (or whatever port you decide to use in the code) in the Exceptions
list.

BTW - the example provided is pretty bare-bones and is subject to
errors if your network is busy. There are better examples that do
proper error checking later on in the book samples. You can freely
download the other sample code from the Sybex web site listed in my
sig. Good luck, and happy network programming!

Rich Blum - Author
"C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176
 
Back
Top