Results 1 to 5 of 5
-
21st Sep 2010, 12:31 PM #1OPMember
C# Client/Server Socket Connection
Hello
I am creating a Client/Server app.
I made all the classes but however when i have multi-threads exceptions are given, there is something wrong with my multicon... any tips ?cgworld Reviewed by cgworld on . C# Client/Server Socket Connection Hello I am creating a Client/Server app. I made all the classes but however when i have multi-threads exceptions are given, there is something wrong with my multicon... any tips ? Rating: 5
-
21st Sep 2010, 01:17 PM #2Member
What exactly do you want if I may ask? More technical information or code snippets?
-
21st Sep 2010, 02:35 PM #3Respected Developer
Fairly simple. Here's a 5-minute multi-threaded example I made:
Server source:
PHP Code:using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace Hyperz.TcpExample.Server
{
public class Program
{
// config
private static IPAddress ip = IPAddress.Any;
private static short port = 8000;
// end of config
private static Thread thread;
private static TcpListener listener;
static void Main(string[] args)
{
Console.Title = "TCP Server";
listener = new TcpListener(ip, port);
thread = new Thread(worker);
// start the listening thread
thread.Start();
}
static void worker()
{
// listen...
listener.Start();
Console.WriteLine("Waiting for clients!");
while (true)
{
// wait for a client to connect
TcpClient client = listener.AcceptTcpClient();
// client connected
Console.WriteLine("Client {0:x} connected...", client.GetHashCode());
// launch a thread to handle it
ThreadPool.QueueUserWorkItem(tcpClient =>
{
var theClient = (TcpClient)tcpClient;
var stream = theClient.GetStream();
var bufferSize = 4096;
var buffer = new byte[bufferSize];
int count;
// read the client data
while (true)
{
count = 0;
try
{
count = stream.Read(buffer, 0, bufferSize);
}
catch (Exception ex)
{
Console.WriteLine("Error while reading client data: " + ex.Message);
break;
}
if (count > 0)
{
// print the data to the console
Console.WriteLine("{0:x} says: {1}", theClient.GetHashCode(),
Encoding.ASCII.GetString(buffer, 0, count));
}
else break;
}
stream.Close();
theClient.Close();
}, client);
}
}
}
}
PHP Code:using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace Hyperz.TcpExample.Client
{
public class Program
{
// config
private static string ip = "127.0.0.1";
private static short port = 8000;
// end of config
private static TcpClient client;
private static IPEndPoint endPoint;
static void Main(string[] args)
{
Console.Title = "TCP Client";
endPoint = new IPEndPoint(IPAddress.Parse(ip), port);
client = new TcpClient();
// connect to our server
try
{
client.Connect(endPoint);
}
catch
{
Console.WriteLine("Could not connect to the server :(");
Console.Read();
return;
}
var stream = client.GetStream();
string msg;
byte[] buffer;
// Send messages to the server
while (true)
{
Console.Write("Message: ");
msg = Console.ReadLine();
if (!String.IsNullOrWhiteSpace(msg))
{
buffer = Encoding.ASCII.GetBytes(msg);
stream.Write(buffer, 0, buffer.Length);
stream.Flush();
}
else break;
}
stream.Close();
client.Close();
Console.WriteLine("Connection closed.");
Console.Read();
}
}
}
-
21st Sep 2010, 07:21 PM #4MemberWebsite's:
Elite.SO Defendos.com Motionite.comGoodwork Hyperz
-
26th Sep 2010, 11:10 AM #5OPMember
Thanks, somehow i managed to do a connection multi connection .
Thanks again
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
Need help set up connection for openvpn server
By mazyar in forum Server ManagementReplies: 9Last Post: 3rd Feb 2012, 11:22 PM -
How many client i able to host in this server?
By raihan0888 in forum Hosting DiscussionReplies: 12Last Post: 23rd Apr 2011, 09:25 PM -
Password-Less OpenSSH [Server-client]
By faizulhaque in forum Server ManagementReplies: 4Last Post: 2nd Apr 2011, 06:50 PM -
Your Fav OS (Both Server and client side)
By chrishdman87 in forum Polling PlazaReplies: 37Last Post: 15th Jan 2011, 01:43 PM -
VPN Server and client for Windows XP
By NewEraCracker in forum Technical Help Desk SupportReplies: 2Last Post: 26th Nov 2010, 11:30 AM
themaLeecher - leech and manage...
Version 5.03 released. Open older version (or...