TPC Server, Client выход в интернет

 
0
 
.NET
ava
KLUBS | 14.12.2007, 19:57
Пробелма в том что при работе у меня на компе, все работает как надоЮ, а при попытке работы с разным компов ничего не работает, выдает ошибку что сервер не ответил....

вот код сервера:

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

namespace SERVER
{
public partial class frmMain : Form
{
string server = "";
Socket serversocket;
private string login_client_machine;
Socket clientsock = null;
string[] cmdList = null;
private int total_clients_connected = 0;

public frmMain()
{
InitializeComponent();

server = Dns.GetHostName();
Text = "[" + server + "]";
}

public void ListenForPeers()
{
try
{
serversocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//serversocket.Blocking = true;

IPHostEntry IPHost = Dns.Resolve(server);
string[] aliases = IPHost.Aliases;
IPAddress[] addr = IPHost.AddressList;
UpdateCaption("SERVER " + addr[0].ToString() + ":8090");
IPEndPoint ipepServer = new IPEndPoint(addr[0], 8090);
serversocket.Bind(ipepServer);
serversocket.Listen(65635);
UpdateStatus("Server Status: ON");
while (true)
{
clientsock = serversocket.Accept();
if (clientsock.Connected)
{
UpdateConnections(++total_clients_connected);
AddMainMessage("Find new connection...");
Thread tc = new Thread(new ThreadStart(listenclient));
tc.Start();
}
}
}
catch (SocketException se)
{
AddMainMessage(se.Message);
}
catch (Exception eee)
{
AddMainMessage(eee.Message);
MessageBox.Show("Socket Connect Error.\n\n" + eee.Message + "\nPossible Cause: Server Already running. Check the tasklist for running processes", "Startup Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
//Application.Exit();
}
}

void listenclient()
{
Socket sock = clientsock;
string cmd = server;
byte[] sender = System.Text.Encoding.ASCII.GetBytes("SERVER|" + cmd);
sock.Send(sender, sender.Length, 0);

try
{
while (sock != null)
{
cmd = "";
byte[] recs = new byte[32767];
int rcount = sock.Receive(recs, recs.Length, 0);
string clientmessage = System.Text.Encoding.ASCII.GetString(recs);
clientmessage = clientmessage.Substring(0, rcount);

string smk = clientmessage;

cmdList = null;
cmdList = clientmessage.Split('|');
string execmd = cmdList[0];
AddMainMessage("COMMAND==>" + execmd);

sender = null;
sender = new Byte[32767];

string parm1 = "";
if (execmd == "USER")
{

login_client_machine = cmdList[1];
string ip = cmdList[2];
AddMainMessage("Client Connected IP:" + ip + " User :" + login_client_machine);
continue;
}

if (execmd == "DISCONNECT")
{
cmd = "CLOSE";
sender = System.Text.Encoding.ASCII.GetBytes(cmd);
sock.Send(sender, sender.Length, 0);

sock.Close();
AddMainMessage("Client Disconnected");
continue;
}
}
}
catch (Exception Se)
{
AddMainMessage(Se.Message);
}
}

void UpdateCaption(string text)
{
Invoke((MethodInvoker)delegate
{
Text = text;
});
}

void UpdateStatus(string text)
{
groupBoxStatus.Invoke((MethodInvoker)delegate
{
groupBoxStatus.Text = text;
});
}

void UpdateConnections(int con)
{
labelConnections.Invoke((MethodInvoker)delegate
{
labelConnections.Text = total_clients_connected.ToString();
});
}

void AddMainMessage(string text)
{
listBoxMainMessages.Invoke((MethodInvoker)delegate
{
listBoxMainMessages.Items.Add(text);
});
}

private void buttonStart_Click(object sender, EventArgs e)
{
Thread th = new Thread(new ThreadStart(ListenForPeers));
th.Start();

buttonStart.Enabled = false;
buttonStop.Enabled = true;
}

private void buttonStop_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}


а вот код клиента:


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

namespace CLIENT
{
public partial class frmMain : Form
{
string server = "";
Socket serversocket;
Socket clientsock = null;
string[] cmdList = null;

public frmMain()
{
InitializeComponent();

server = Dns.GetHostName();
Text = "[" + server + "]";
}

private void buttonConnect_Click(object sender, EventArgs e)
{
try
{
serversocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serversocket.Blocking = true;

IPHostEntry IPHost = Dns.Resolve(textBoxIP.Text);
string[] aliases = IPHost.Aliases;
IPAddress[] addr = IPHost.AddressList;

IPEndPoint ipepServer = new IPEndPoint(addr[0], 8090);
serversocket.Connect(ipepServer);
clientsock = serversocket;

Thread MainThread = new Thread(new ThreadStart(listenclient));
MainThread.Start();

buttonConnect.Enabled = false;
buttonDisconnect.Enabled = true;
}
catch (SocketException se)
{
AddMainMessage(se.Message);
}
catch (Exception eee)
{
AddMainMessage(eee.Message);
MessageBox.Show("Socket Connect Error.\n\n" + eee.Message + "\nPossible Cause: Server Already running. Check the tasklist for running processes", "Startup Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
//Application.Exit();
}
}

void listenclient()
{
Socket sock = clientsock;
string cmd = server;
byte[] sender = System.Text.Encoding.ASCII.GetBytes("CLIENT|" + cmd);
sock.Send(sender, sender.Length, 0);

try
{
while (sock != null)
{
cmd = "";
byte[] recs = new byte[32767];
int rcount = sock.Receive(recs, recs.Length, 0);
string clientmessage = System.Text.Encoding.ASCII.GetString(recs);
clientmessage = clientmessage.Substring(0, rcount);

string smk = clientmessage;

cmdList = null;
cmdList = clientmessage.Split(' ');
string execmd = cmdList[0];
AddMainMessage("COMMAND==>" + execmd);

sender = null;
sender = new Byte[32767];


string parm1 = "";
if (execmd == "SERVER")
{
AddMainMessage("Connected To Server :" + cmdList[1]);
continue;
}

if (execmd == "CLOSE")
{
break;
}
}
}
catch (Exception Se)
{
AddMainMessage(Se.Message);
}
}

void AddMainMessage(string text)
{
listBoxMainMessages.Invoke((MethodInvoker)delegate
{
listBoxMainMessages.Items.Add(text);
});
}

private void buttonSendInfo_Click(object sender, EventArgs e)
{
IPHostEntry IPHost = Dns.Resolve(server);
IPAddress[] addr = IPHost.AddressList;
Socket sock = clientsock;

string cmd = "USER|" + textBoxLogin.Text + "|" + addr[0];

byte[] send = System.Text.Encoding.ASCII.GetBytes(cmd);
sock.Send(send, send.Length, 0);
}
}
}



Помогите если сможете smile
Comments (0)

Comments are not added

Please register or login to write.
Firm of day
Вы также можете добавить свою фирму в каталог IT-фирм, и публиковать статьи, новости, вакансии и другую информацию от имени фирмы.
Подробнее
Contributors
  KLUBS
advanced
Submit