Hey there.
I am trying to make a new program, I am new with Sockets because it is kinda hard, and I need your help on making these things [Server and Client]:
I want to send from the server to the client a string and when the client gets the string "doitall" it will show a messagebox on the client.
If you can't help me with that can you please give me something that can help?
a tutorial or something? because I don't know how to make that the client gets a
string too, only the server..
I think I need to do this with Asynchronous Socket
Thanks in advance!
Loading
Kirtan PatelPosted Feb 12, 2010, 5:09 PM
using System.Net.Sockets;
using System.Net;
Server Part Coding
--------------------------
static void Main(string[] args)
{
Console.WriteLine("Enter Ip Address to Connect to Send Data:");
string Ip = Console.ReadLine();
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(Ip), 999);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(ep);
socket.Listen(50);
Console.WriteLine( "Waiting For Client ....");
Socket client = socket.Accept();
IPEndPoint clientEp = (IPEndPoint)client.RemoteEndPoint;
Console.WriteLine("Connected to Client !!");
while(true)
{
Console.WriteLine("Enter String :\n");
string welcome = Console.ReadLine();
byte[] Data = new byte[1024];
Data = Encoding.ASCII.GetBytes(welcome);
client.Send(Data, SocketFlags.None);
}
client.Close();
socket.Close();
Console.ReadKey();
}
Client Part Coding
----------------------
static void Main(string[] args)
{
Console.WriteLine("Enter Ip to Listen:");
string Ip = Console.ReadLine();
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(Ip), 999);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
server.Connect(ipe);
}
catch
{
Console.WriteLine("Unable To Connect to Server ");
}
while (true)
{
byte[] Data = new byte[1024];
int recentDataLength = server.Receive(Data);
string stringData = Encoding.ASCII.GetString(Data, 0, recentDataLength);
Console.WriteLine(stringData);
}
}
Uros BregarPosted Feb 13, 2010, 4:47 AM
Uros
or nakashPosted Feb 12, 2010, 6:44 PM
You are the man!
one more thing,I need to get the IP Address from the client,is there a way to do this? I was thinking about sending the IP in the first connect from the client to server and the server will be listening to this server,but is there another way to get the connected client's IP Address?
Can you help me with the other thing? when someone is typing for example
"pal" it makes a Console.Beep(); event.
And how to make it for many clients? There is only one in your code to listen to ;p