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 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);
}
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.