C#TCP/IP网络编程 [英] C# TCP/IP network progamming
本文介绍了C#TCP/IP网络编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
伙计们,我需要帮助来弄清楚如何使此桌面应用程序使用无线LAN连接到位于另一台计算机上的服务器.应用程序(客户端和服务器)是TCP/IP网络程序.
这是客户端和服务器代码的摘录:
来自客户:
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.
推荐答案
"+用户+"
"+通过; 传出= ASCIIEncoding.ASCII.GetBytes(send); 流myStream = myClient.GetStream(); myStream.Write(outgoing,0,Outing.Length); 为(int i = 0; i< incoming.Length; i ++) { 传入[i] = 0; } myStream.Read(传入,0,传入.长度); MessageBox.Show(ASCIIEncoding.ASCII.GetString(传入,0,传入.长度)); } } }
" + 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()); } } } } }
''}; 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.
这篇关于C#TCP/IP网络编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文