无法运行小型服务器客户端编程 [英] Could not run small server client prog
本文介绍了无法运行小型服务器客户端编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hello
With very low knowledege in server Client program and
in server client program which was posted in codeproject
<a href="https://www.codeproject.com/Articles/1415/Introduction-to-TCP-client-server-in-C"></a>
i could not run this program ,look at those notes
1 - Both programs are run in same machine(64 bit),.NET FrameWork 4.5 window 10
2- I have got the address from my computer using Ms Dos command=ipconfig/all from
Ethernet adapter vEthernet (Default Switch)
the IPv4 address = 172.31.57.17
i have made minor change to the program that listed in the above link
3 - to make my question clear
using CONTROL PANEL in NETWORK CONNECTION ,i noticed that i have 4 icons
one for Ethernet,one for vEthernet(Default Swith) ,one for vEthernet (default switch)2 and last one for wirless
after running the program Client i have got his message
(No connection could be made because the target machine actively refused it 172.31.57.17:8001)
need help
What I have tried:
<pre lang="
/////Server ///////////////////////////////
public partial class Form1 : Form
{
IPAddress ipAd;
TcpListener myList;
public Form1()
{
InitializeComponent();
}
private void FormLoad(object sender, EventArgs e)
{
//ipAd = IPAddress.Parse("172.21.5.99");// as written see the link
ipAd = IPAddress.Parse("172.31.57.17");//i have tried this
//ipAd = IPAddress.Parse("172.31.57.18");// and this
myList = new TcpListener(ipAd, 8001);
}
private void StartServerClick(object sender, EventArgs e)
{
myList.Start();
Console.WriteLine("The server is running at port 8001...");
Console.WriteLine("The local End point is :" + myList.LocalEndpoint);
Console.WriteLine("Waiting for a connection.....");
Socket s = myList.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
byte[] b = new byte[100];
int k = s.Receive(b);
Console.WriteLine("Recieved...");
for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(b[i]));
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes("The string was recieved by the server."));
Console.WriteLine("\nSent Acknowledgement");
s.Close();
myList.Stop();
}
}
///////////////////////////Client //////////////////////////////////////////////////
TcpClient tcpclnt;
public Form1()
{
InitializeComponent();
}
private void FormLoad(object sender, EventArgs e)
{
tcpclnt = new TcpClient();
}
private void StartClient(object sender, EventArgs e)
{
Console.WriteLine("Connecting.....");
tcpclnt.Connect("172.31.57.17", 8001); // use the ipaddress as in the server program
//tcpclnt.Connect("172.31.57.18", 8001);
Console.WriteLine("Connected");
Console.Write("Enter the string to be transmitted : ");
String str=Console.ReadLine();
Stream stm = tcpclnt.GetStream();
ASCIIEncoding asen= new ASCIIEncoding();
byte[] ba=asen.GetBytes(str);
Console.WriteLine("Transmitting.....");
stm.Write(ba,0,ba.Length);
byte[] bb=new byte[100];
int k=stm.Read(bb,0,100);
for (int i=0;i<k;i++)
Console.Write(Convert.ToChar(bb[i]));
tcpclnt.Close();
}
}
">
推荐答案
参见TcpListener.AcceptTcpClient [ ^ ]。您也可以尝试询问撰写文章的人。
See TcpListener.AcceptTcpClient[^] for sample server code. You could also try asking the person who wrote the article.
这篇关于无法运行小型服务器客户端编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文