本文介绍了如何使用c#代码在Windows服务中将客户端从控制台连接到服务器套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 <pre lang="c#">客户代码 试试 { 流程pdf =新流程(); pdf.StartInfo.FileName = @C:\ Users \PC\Documents\\ \\ Visual Studio 2010 \Projects\WindowsFormsApplication1 \webs \webs \serverservice \serverservice\bin\Debug \serverservice.exe; serverservice ss = new serverservice(); ss.myserver(); 计划pp =新计划(); pp.beet(); } catch(例外e) { Console.WriteLine(error ....+ e.StackTrace); } } public void beet() { TcpClient tcpc = new TcpClient(); Console.WriteLine(连接.......); tcpc.Connect(10.128.1.116,80); Console.WriteLine(connected ........); 控制台.Write(输入msg为transimitt); string str = Console.ReadLine(); Stream stm = tcpc.GetStream(); ASCIIEncoding asc = new ASCIIEncoding(); byte [] ba = asc.GetBytes(str); Console.WriteLine(transmit ..........); stm .Write(ba,0,ba.Length); byte [] bb = new byte [100]; int k = stm.Read(bb,0,100); for(int i = 0;我< K表; i ++) Console.Write(Convert.ToChar(bb [i])); tcpc.Close(); } windows服务代码 protected override void OnStart(string [] args) { 线程mythread =新线程(新的ThreadStart(myserver)); mythread.Start() ; //处理pdf1 =新流程(); //pdf1.StartInfo.FileName = @C:\ Users \PC\Documents \ Visual Studio 2010 \Projects\WindowsFormsApplication1\webs\webs\clientconsole\clientconsole\bin\Debug\clientconsole.exe; //pdf1.Start(); } public void myserver() { char d; IPAddress ipad1 = IPAddress.Parse(10.128.1.116); TcpListener mylist1 = new TcpListener(ipad1,80); mylist1.Start(); Console.WriteLine (服务器在端口80运行); Console.WriteLine(本地端点是+ mylist1.LocalEndpoint); Console.WriteLine(等待连接); // ServiceController ser = new 套接字s = mylist1.AcceptSocket(); Console.WriteLine(接受来自+ s.RemoteEndPoint); byte [] b =新字节[100]; int k = s.Receive(b); Console.WriteLine(收到); for(int i = 0;我< K表; i ++) { d = Convert.ToChar(b [i]); Console.Write(char.ToUpper(d)); } ASCIIEncoding asc = new ASCIIEncoding(); s.Send(asc.GetBytes( msg被传递)); Console.WriteLine(\ n发送aknwldge); s.Close(); mylist1.Stop(); Console.ReadLine(); } protected override void OnStop() { EventLog.WriteEntry(已停止) ; } } } client code try { Process pdf = new Process(); pdf.StartInfo.FileName = @"C:\Users\PC\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\webs\webs\serverservice\serverservice\bin\Debug\serverservice.exe"; serverservice ss = new serverservice(); ss.myserver(); Program pp = new Program(); pp.beet(); } catch (Exception e) { Console.WriteLine("error...." + e.StackTrace); } } public void beet() { TcpClient tcpc = new TcpClient(); Console.WriteLine("connecting......."); tcpc.Connect("10.128.1.116", 80); Console.WriteLine("connected........"); Console.Write("enter msg to be transimitt"); string str = Console.ReadLine(); Stream stm = tcpc.GetStream(); ASCIIEncoding asc = new ASCIIEncoding(); byte[] ba = asc.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])); tcpc.Close(); }windows service code protected override void OnStart(string[] args) { Thread mythread = new Thread(new ThreadStart(myserver)); mythread.Start(); //Process pdf1 = new Process(); //pdf1.StartInfo.FileName = @"C:\Users\PC\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\webs\webs\clientconsole\clientconsole\bin\Debug\clientconsole.exe"; //pdf1.Start(); } public void myserver() { char d; IPAddress ipad1 = IPAddress.Parse("10.128.1.116"); TcpListener mylist1 = new TcpListener(ipad1, 80); mylist1.Start(); Console.WriteLine("server is running at port 80"); Console.WriteLine("local end point is" + mylist1.LocalEndpoint); Console.WriteLine("waiting for connection"); // ServiceController ser=new Socket s = mylist1.AcceptSocket(); Console.WriteLine("connection is accepted from" + s.RemoteEndPoint); byte[] b = new byte[100]; int k = s.Receive(b); Console.WriteLine("received"); for (int i = 0; i < k; i++) { d = Convert.ToChar(b[i]); Console.Write(char.ToUpper(d)); } ASCIIEncoding asc = new ASCIIEncoding(); s.Send(asc.GetBytes("msg is recveived")); Console.WriteLine("\n send aknwldge"); s.Close(); mylist1.Stop(); Console.ReadLine(); } protected override void OnStop() { EventLog.WriteEntry("stopped"); } }}推荐答案 这篇关于如何使用c#代码在Windows服务中将客户端从控制台连接到服务器套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-30 04:16