hi all, i have the server code and client code, but it cannot communicating with each other. i try to run it in 2 pc but happen error. i do not know what is the problem, please help me.
server code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net; //networking
using System.Net.Sockets; //networking
using System.Threading; //networking
using System.Net.NetworkInformation; //monitor bandwidth
namespace NBCS_serverlogin
{
public partial class NBCSadmin : Form
{
private TcpListener tcpListener;
private Thread threadTcp;
static NetworkStream clientNetStream;
const int portnumber = 3333;
// public delegate void ChangedEventHandler(object sender, EventArgs e);
//public event ChangedEventHandler Changed;
public NBCSadmin()
{
InitializeComponent();
portnotxt.Text = "3333";
//initiateListen();
}
private void initiateListen()
{
//Tcp
this.tcpListener = new TcpListener(IPAddress.Any, portnumber);
System.Convert.ToInt16(portnotxt.Text.Trim());
this.threadTcp = new Thread(new ThreadStart(ListenToClients));
// this.threadTcp.Start();
}
//listen for connected client
private void ListenToClients()
{
tcpListener.Start();
while (true)
{
try
{
// block till client connect to server
TcpClient clientCom = this.tcpListener.AcceptTcpClient();
//handle connected clients
Thread threadClient = new Thread(new ParameterizedThreadStart(HandleClients));
threadClient.Start(clientCom);
}
catch (Exception)
{
MessageBox.Show ("error");
}
}
}
private void HandleClients(object clientCom)
{
TcpClient tcpClient = (TcpClient)clientCom;
clientNetStream = tcpClient.GetStream();
byte[] message = new byte[4096];
int bytesRead;
while (true)
{
bytesRead = 0;
try
{
bytesRead = clientNetStream.Read(message, 0, 4096);
}
catch (Exception)
{
// error happened
break;
}
if (bytesRead == 0)
{
// client diconnected to server
constatuslabel.Text = "Client has disconnected from the server.";
break;
}
//msg has been successfully received
ASCIIEncoding asciiEncoder = new ASCIIEncoding();
// output msg
constatuslabel.Text = tcpClient.Client.LocalEndPoint + "" + tcpClient.Client.RemoteEndPoint + asciiEncoder.GetString(message, 0, bytesRead);
//System.Diagnostics.Debug.WriteLine(asciiEncoder.GetString(message, 0, bytesRead));
}
// (button)
tcpClient.Close();
}
private void connecttoclientbtn_Click_1(object sender, EventArgs e)
{
initiateListen();
constatuslabel.Text = "Waiting for a connection...";
}
private void portnotxt_TextChanged(object sender, EventArgs e)
{
}
}
}
client code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
namespace NBCS_Client
{
public partial class NBCSuser : Form
{
private TcpClient clientuser;
private IPEndPoint serverEndPoint;
private NetworkStream clientStream;
const int portnumber = 3333;
public NBCSuser()
{
InitializeComponent();
// initiateListenUser();
ipaddtxt.Text = "127.0.0.1";
portnotxt.Text = "3333";
}
public void initiateListenUser()
{
clientuser = new TcpClient();
//get the remote ip add
IPAddress ipaddress = IPAddress.Parse(ipaddtxt.Text.Trim());
System.Convert.ToInt16(portnotxt.Text.Trim());
//create end point
serverEndPoint = new IPEndPoint(ipaddress, portnumber);
}
public void connectTcpServer()
{
clientuser.Connect(serverEndPoint); //10061 error
clientStream = clientuser.GetStream();
//create thread to handle communication with connected client
Thread threadClient = new Thread(new ParameterizedThreadStart(HandleClients));
threadClient.Start(clientuser);
}
public void HandleClients(object clientuser)
{
TcpClient tcpclient = (TcpClient)clientuser;
NetworkStream clientStream = tcpclient.GetStream();
byte[] message = new byte[4096];
int bytesRead;
while (true)
{
bytesRead = 0;
try
{
//block until client send a msg
bytesRead = clientStream.Read(message, 0, 4096);
}
catch (SocketException)
{
//socket error
break;
}
if (bytesRead == 0)
{
//client disconnected to server
break;
}
//msg received successfully
ASCIIEncoding asciiEncoder = new ASCIIEncoding();
// output msg
userconstatuslabel.Text = tcpclient.Client.LocalEndPoint + "" + tcpclient.Client.RemoteEndPoint + asciiEncoder.GetString(message, 0, bytesRead);
//textFrmServer
}
tcpclient.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void exitbtn_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void connectbtn_Click(object sender, EventArgs e)
{
initiateListenUser();
connectTcpServer();
userconstatuslabel.Text = "Connecting to server...";
}
}
}