求大神帮我看看程序,

问题描述

我想实现在form4上通信,socket,tcp,按键1发送open,按键2发送close但是我的有问题。。哎usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingSystem.Net.Sockets;usingSystem.Threading;usingSystem.Net;namespacehh3{publicpartialclassForm3:Form{privatestaticbyte[]result=newbyte[1024];privatestaticintmyProt=8080;//端口staticSocketserverSocket;//privateThreadtd;//privateTcpListenertcpListener;//privatestaticstringmessage="";//privateIPAddressMyIP=IPAddress.Parse("127.0.0.1");publicForm3(){InitializeComponent();timer1.Enabled=true;timer1.Interval=10;}privatevoidForm3_Load(objectsender,EventArgse){//td=newThread(newThreadStart(this.StartListen));stringhostName=Dns.GetHostName();//本机名IPAddressip=IPAddress.Parse("172.26.228.1");System.Net.IPAddress[]addressList=Dns.GetHostAddresses(hostName);//会返回所有地址,包括IPv4和IPv6//IPAddressip=IPAddress.Parse(addressList);serverSocket=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);serverSocket.Bind(newIPEndPoint(ip,myProt));//绑定IP地址:端口serverSocket.Listen(10);//设定最多10个排队连接请求//Console.WriteLine("启动监听{0}成功",serverSocket.LocalEndPoint.ToString());//通过Clientsoket发送数据}privatestaticvoidReceiveMessage(objectclientSocket){SocketmyClientSocket=(Socket)clientSocket;while(true){try{//通过clientSocket接收数据intreceiveNumber=myClientSocket.Receive(result);//Console.WriteLine("接收客户端{0}消息{1}",myClientSocket.RemoteEndPoint.ToString(),Encoding.ASCII.GetString(result,0,receiveNumber));}catch(Exceptionex){Console.WriteLine(ex.Message);myClientSocket.Shutdown(SocketShutdown.Both);myClientSocket.Close();break;}}}privatestaticvoidListenClientConnect1(){SocketclientSocket=serverSocket.Accept();clientSocket.Send(Encoding.ASCII.GetBytes("open"));ThreadreceiveThread1=newThread(ReceiveMessage);receiveThread1.Start(clientSocket);receiveThread1.Abort();//clientSocket.Close();}privatestaticvoidListenClientConnect2(){SocketclientSocket=serverSocket.Accept();clientSocket.Send(Encoding.ASCII.GetBytes("close"));ThreadreceiveThread2=newThread(ReceiveMessage);receiveThread2.Start(clientSocket);receiveThread2.Abort();}privatevoidlabel1_Click(objectsender,EventArgse){}privatevoidtextBox3_TextChanged(objectsender,EventArgse){}privatevoidtimer1_Tick(objectsender,EventArgse){DateTimetime=DateTime.Now;textBox3.Text=time.ToString();}privatevoidbutton1_Click(objectsender,EventArgse){//button1.Enabled=false;//button2.Enabled=true;ThreadmyThread1=newThread(ListenClientConnect1);myThread1.Start();myThread1.Abort();//Console.ReadLine();}privatevoidbutton2_Click(objectsender,EventArgse){//button1.Enabled=true;//button2.Enabled=false;ThreadmyThread2=newThread(ListenClientConnect2);myThread2.Start();myThread2.Abort();//Console.ReadLine();}}}

解决方案

解决方案二:
就是现在和网络调试助手连接后只能发一次数据,第二次就要断开重新连接

时间: 2024-08-03 22:22:00

求大神帮我看看程序,的相关文章

字符串处理-求大神帮解决如下程序,最基本的C语言字符串类型,不用编太难(如下为问题要求,测试用例,输出用例)

问题描述 求大神帮解决如下程序,最基本的C语言字符串类型,不用编太难(如下为问题要求,测试用例,输出用例) Background Given an m by n grid of letters and a list of words, find the location in the grid at which the word can be found. A word matches a straight, uninterrupted line of letters in the grid.

c++-求大神帮我把这机构化的C++程序改成C++面向对象的程序,有酬谢(单项选择题标准化考试系统)

问题描述 求大神帮我把这机构化的C++程序改成C++面向对象的程序,有酬谢(单项选择题标准化考试系统) #include<stdio.h>#include<stdlib.h>//应用动态存储分配函数//#include<time.h># define LEN sizeof(struct question)struct question{ char ask[200];//选择题题目// char answer[4][80];//选择题选项每个答案的长度// int rig

总是不出结果-求大神帮我看看 这c++程序问题出在哪里

问题描述 求大神帮我看看 这c++程序问题出在哪里 // BaseShape.h文件#include using namespace std; class BaseShape { public: BaseShape() {}; virtual ~BaseShape() {}; virtual void DrawShape() = 0; }; //*********************************//ShapeFactory.h文件 #include #include #inclu

求大神帮我解释几句程序,图书管理系统读者部分的。

问题描述 求大神帮我解释几句程序,图书管理系统读者部分的. int Reader::select_Library(Chaxun & se)//图书馆在册书查询{ MYSQL_RES * point; int bb[7]={14145141244}; string aa=""select book_name as '书名'ISBNwriter as '作者'publish as '出版社'publish_time as '出版日期' price as '价格'maxbook-nu

求大神帮小女子指导一下程序,遥控按键按下 1602 无法显示按键值

问题描述 求大神帮小女子指导一下程序,遥控按键按下 1602 无法显示按键值 /*----------------------------------------------- 名称:遥控器红外解码数液晶显示 ------------------------------------------------*/ #include #include #define uchar unsigned char #define uint unsigned int uchar i,a,b,c,d; uint

求大神帮我改改c++程序,总是停止运行!

问题描述 求大神帮我改改c++程序,总是停止运行! #include using namespace std; class Fraction { private: int num,den; void normalize(); int gcf(int a,int b); int lcm(int a,int b); public: Fraction() {set(0,1);cout<<"construct"<<endl;} Fraction(int n,int d)

编程语言学习-求大神帮我看看这程序哪里有问题 运行的时候有时候会出现相同的票数

问题描述 求大神帮我看看这程序哪里有问题 运行的时候有时候会出现相同的票数 package com.homework; public class BuyTickets1 implements Runnable { static int tickets = 30; private String name; public BuyTickets1(String name) { this.name = name; } public void run() { for (int i = 1; i <= 35

VS2012无法附加进程,求大神帮解决

问题描述 VS2012无法附加进程,求大神帮解决 解决方案 直接在vs里调试你的asp.net程序

新生 求大神帮帮忙!

问题描述 新生 求大神帮帮忙! 解决方案 求大神帮帮忙 解决方案二: 先看看你的数据库启动了没 解决方案三: 试试: 打开'程序'-'所有程序'-'Microsoft SQL Server 2012 '-'配置工具'-'SQL Server 配置管理器',在弹出的窗体中,找到'SQL Server 2012 网络配置',把'MSSQLSERVER的协议'下的"Named Pipes"和"TCP/IP"启动,然后重新启动Microsoft SQL Server 201