Click here to Skip to main content
15,561,685 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Guys pls i need help figuring how to make this desktop application connect to the server located in another computer using wireless LAN. The applications( the client and the server) are TCP/IP network programs.
this is an extract from the client and server codes:

from client:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace TCP_test_client
{
    public partial class Form1 : Form
    {
        const int buffer = 5000;
        const int port = 1759;
        const string LocalHost = "localhost";
        byte[] incoming = new byte[buffer];
        byte[] outgoing = new byte[buffer];
        
        public Form1()
        {
            InitializeComponent();            
        }

        private void connect()
        {
            TcpClient myC = new TcpClient(LocalHost, port);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string user = textBox1.Text;
            string pass = textBox2.Text;
            string send = string.Empty;

            TcpClient myClient = new TcpClient(LocalHost, port);
            MessageBox.Show("Connected");

            send = "login$" + user + "$" + pass;

            outgoing = ASCIIEncoding.ASCII.GetBytes(send);
            Stream myStream = myClient.GetStream();
            myStream.Write(outgoing, 0, outgoing.Length);

            for (int i = 0; i < incoming.Length; i++)
            {
                incoming[i] = 0;
            }

            myStream.Read(incoming, 0, incoming.Length);
            MessageBox.Show(ASCIIEncoding.ASCII.GetString(incoming, 0, incoming.Length));
    
        }
    }
}


from server:

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

namespace Conversion_server
{
    class Program
    {
        static void Main(string[] args)
        {
            const int buffer = 5000;
            const int port = 1759;
            //var ipAddress = new IPAddress(new byte[] { 127, 0, 0, 1 });
            var Ascii = new ASCIIEncoding();
            var incoming = new byte[buffer];
            var outgoing = new byte[buffer];
            string data = string.Empty;
            string[] details = new string[3];
            

            string machineHostName = string.Empty;
            IPAddress[] machineIP = null;

            try
            {
                machineHostName = Dns.GetHostName();
                IPHostEntry ipEntry = Dns.GetHostByName(machineHostName);
                machineIP = ipEntry.AddressList;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error trying to get local address {0} ", ex.Message);
            }

            // Verify we got an IP address. Tell the user if we did
            if (machineIP == null || machineIP.Length < 1)
            {
                Console.WriteLine("Unable to get local address");
                return;
            }

            var TCP = new TcpListener(machineIP[0], port);
            TCP.Start();
            Console.WriteLine("***** Server Started {0} *****", DateTime.Now.ToShortTimeString());

            Console.WriteLine("Listening on : [{0}] {1}:{2}", machineHostName, machineIP[0], port);

            Boolean exit = true;

            
            while (exit)
            {
                try
                {
                    Socket sock = TCP.AcceptSocket();
                    int count = sock.Receive(incoming, incoming.Length, 0);
                    data = ASCIIEncoding.ASCII.GetString(incoming, 0, count);
                    
                    char[] delimiters = {'$'};
                    details = data.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);

                    Console.WriteLine(DateTime.Now.ToShortTimeString() + " : {0}", details[0]);
                    data = details[0].ToLower();

                    for (int i = 0; i < incoming.Length; i++)
                        incoming[i] = 0;

                    Console.WriteLine("[{0}] *** Username : {1}\n[{2}] *** Password : {3}", DateTime.Now.ToShortTimeString(), details[1], DateTime.Now.ToShortTimeString(),details[2]);

                    if (data.Equals("login"))
                    {
                        if (details[1] == "johnemg" && details[2] == "donhongkong")
                        {
                            outgoing = ASCIIEncoding.ASCII.GetBytes("The user is registered as : Emejulu Tochukwu Valentine");
                            sock.Send(outgoing, outgoing.Length, 0);
                        }
                        else
                        {
                            outgoing = ASCIIEncoding.ASCII.GetBytes("The user is not registered");
                            sock.Send(outgoing, outgoing.Length, 0);
                        }

                        
                    }              
                }
                catch (SocketException sockEx)
                {
                    Console.WriteLine("Generic Exception Message: {0}", sockEx.ToString());
                }
            }       
        }
    }
}


Please someone help me with a way to proceed.
Thanks in advance.
Posted
Comments
Sergey Alexandrovich Kryukov 14-Jan-12 4:37am    
What's the problem?
--SA
Sergey Alexandrovich Kryukov 14-Jan-12 4:40am    
Do you want to serve just one client, it looks like?
--SA

C#
        const string LocalHost = "localhost";
...
            TcpClient myC = new TcpClient(LocalHost, port);

Wouldn't it be better to try making a connection to the server?
 
Share this answer
 
Comments
Emejulu JVT 14-Jan-12 4:56am    
That's the much i know in working with sockets. Please is there a better way to do this.
Richard MacCutchan 14-Jan-12 5:05am    
Not that I know of, try using Google for "socket programming" and you will find lots of useful information.
OK, you did not share with us what is your problem, but this code just don't look serious. You cannot seriously develop TCP communication without threading.

You can get some idea from my past solution: Multple clients from same port Number[^].

—SA
 
Share this answer
 
Comments
Emejulu JVT 14-Jan-12 5:22am    
I think those are too advanced for me now. But if u can help me with links to ebooks, i'll like to read up. I'll tried but finding some useless books.
Sergey Alexandrovich Kryukov 25-Sep-12 13:11pm    
If this is too advanced, get to some simpler tasks for now, to gain some knowledge and confidence. How else could you get help?
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900