Activity Stream
48,167 MEMBERS
61226 ONLINE
besthostingforums On YouTube Subscribe to our Newsletter besthostingforums On Twitter besthostingforums On Facebook besthostingforums On facebook groups

Results 1 to 5 of 5
  1.     
    #1
    Member

    Default 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

  2.   Sponsored Links

  3.     
    #2
    Member
    What exactly do you want if I may ask? More technical information or code snippets?

  4.     
    #3
    Respected 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(ipport);
                
    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(buffer0bufferSize);
                            }
                            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(buffer0count));
                            }
                            else break;
                        }

                        
    stream.Close();
                        
    theClient.Close();

                    }, 
    client);
                }
            }
        }

    Client 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.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(buffer0buffer.Length);
                        
    stream.Flush();
                    }
                    else break;
                }

                
    stream.Close();
                
    client.Close();

                
    Console.WriteLine("Connection closed.");
                
    Console.Read();
            }
        }

    Screeny:

  5.     
    #4
    Member
    Website's:
    Elite.SO Defendos.com Motionite.com
    Goodwork Hyperz

    Defendos BETA3 Released! Thread - Official Website

  6.     
    #5
    Member
    Thanks, somehow i managed to do a connection multi connection .

    Thanks again

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Need help set up connection for openvpn server
    By mazyar in forum Server Management
    Replies: 9
    Last Post: 3rd Feb 2012, 11:22 PM
  2. How many client i able to host in this server?
    By raihan0888 in forum Hosting Discussion
    Replies: 12
    Last Post: 23rd Apr 2011, 09:25 PM
  3. Password-Less OpenSSH [Server-client]
    By faizulhaque in forum Server Management
    Replies: 4
    Last Post: 2nd Apr 2011, 06:50 PM
  4. Your Fav OS (Both Server and client side)
    By chrishdman87 in forum Polling Plaza
    Replies: 37
    Last Post: 15th Jan 2011, 01:43 PM
  5. VPN Server and client for Windows XP
    By NewEraCracker in forum Technical Help Desk Support
    Replies: 2
    Last Post: 26th Nov 2010, 11:30 AM

Tags for this Thread

BE SOCIAL