Jump to content

C# - project on server - client system --HELP!!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
JDomo

JDomo

    Newbie

  • Members
  • Pip
  • 7 posts
Hello everyone,

I need alot of help from anyone of you. As i am totally suck with programming, and i was given a project to do on server - client system using C#.

I was able to get some information on 1 server to 1 client programming. but i need to write a program on 1 server to many clients.. I think it is called MultiThreads.

Hope someone can guide me through and help me up with some sample or links for my reference..

Thanks..

Newbie in C#
JDomo

#2
PGP_Protector

PGP_Protector

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 253 posts
What do you have so far.
Any outline ?
Do you know how to start new threads ?
Do you know how to have one client talk to one server ?

#3
JDomo

JDomo

    Newbie

  • Members
  • Pip
  • 7 posts
Hi Protector,

Oh ya, i totally duno about anything, i got the sample code on this some link..

/* Server Program */

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;

public class serv {
public static void Main() {
try {
IPAddress ipAd = IPAddress.Parse("172.21.5.99");
// use local m/c IP address, and
// use the same in the client

/* Initializes the Listener */
TcpListener myList=new TcpListener(ipAd,8001);

/* Start Listeneting at the specified port */
myList.Start();

Console.WriteLine("The server is running at port 8001...");
Console.WriteLine("The local End point is :" +
myList.LocalEndpoint );
Console.WriteLine("Waiting for a connection.....");

Socket s=myList.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);

byte[] b=new byte[100];
int k=s.Receive(b);
Console.WriteLine("Recieved...");
for (int i=0;i<k;i++)
Console.Write(Convert.ToChar(b[i]));

ASCIIEncoding asen=new ASCIIEncoding();
s.Send(asen.GetBytes("The string was recieved by the server."));
Console.WriteLine("\nSent Acknowledgement");
/* clean up */
s.Close();
myList.Stop();

}
catch (Exception e) {
Console.WriteLine("Error..... " + e.StackTrace);
}
}

}

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

/* Client Program */

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Sockets;


public class clnt {

public static void Main() {

try {
TcpClient tcpclnt = new TcpClient();
Console.WriteLine("Connecting.....");

tcpclnt.Connect("172.21.5.99",8001);
// use the ipaddress as in the server program

Console.WriteLine("Connected");
Console.Write("Enter the string to be transmitted : ");

String str=Console.ReadLine();
Stream stm = tcpclnt.GetStream();

ASCIIEncoding asen= new ASCIIEncoding();
byte[] ba=asen.GetBytes(str);
Console.WriteLine("Transmitting.....");

stm.Write(ba,0,ba.Length);

byte[] bb=new byte[100];
int k=stm.Read(bb,0,100);

for (int i=0;i<k;i++)
Console.Write(Convert.ToChar(bb[i]));

tcpclnt.Close();
}

catch (Exception e) {
Console.WriteLine("Error..... " + e.StackTrace);
}
}

}


The above sample is a 1 to 1 server client system.

wondering if u can help/tell me how to change it into a MULTI-Threads server - clients system..?

JDomo
The Newbie...