服务端usingSystem;usingSystemCollectionsGeneric;usingSystemText;usingSystemNet;usingSystemNetSockets;namespaceSocketSer{classProgram{[STAThread]staticvoidMain(string[]args){intrecv;byte[]data=newbyte[1024];IPEndPointipep=newIPEndPoint(IPAddressAny,9050);Socketnewsock=newSocket(AddressFamilyInterNetwork,SocketTypeStream,ProtocolTypeTcp);newsockBind(ipep);newsockListen(10);ConsoleWriteLine("等待客户端连接中。。。");Socketclient=newsockAccept();IPEndPointclientip=(IPEndPoint)clientRemoteEndPoint;ConsoleWriteLine("已连接的客户端:"+clientipAddress+",端口"+clientipPort);stringwelcome="welcomehere!";data=EncodingASCIIGetBytes(welcome);clientSend(data,dataLength,SocketFlagsNone);//发送信息while(true){//用死循环来不断的从客户端获取信息data=newbyte[1024];recv=clientReceive(data);ConsoleWriteLine("recv="+recv);if(recv==0)//当信息长度为0,说明客户端连接断开break;ConsoleWriteLine(EncodingASCIIGetString(data,0,recv));clientSend(data,recv,SocketFlagsNone);}ConsoleWriteLine("已断开从"+clientipAddress+"的连接。");clientClose();newsockClose();}}}客户端usingSystem;usingSystemCollectionsGeneric;usingSystemText;usingSystemNet;usingSystemNetSockets;namespaceSocketCli{classProgram{[STAThread]staticvoidMain(string[]args){////TODO:在此处添加代码以启动应用程序//byte[]data=newbyte[1024];Socketnewclient=newSocket(AddressFamilyInterNetwork,SocketTypeStream,ProtocolTypeTcp);//ConsoleWrite("请输入服务器");//stringipadd=ConsoleReadLine();//ConsoleWriteLine();//ConsoleWrite("pleaseinputtheserverport:");//intport=ConvertToInt32(ConsoleReadLine());IPEndPointie=newIPEndPoint(IPAddressParse("19216812"),9050);//服务器的IP和端口try{//因为客户端只是用来向特定的服务器发送信息,所以不需要绑定本机的IP和端口。不需要监听。newclientConnect(ie);}catch(SocketExceptione){ConsoleWriteLine("未连接服务器");ConsoleWriteLine(eToString());ConsoleReadLine();return;}intrecv=newclientReceive(data);stringstringdata=EncodingASCIIGetString(data,0,recv);ConsoleWriteLine(stringdata);while(true){stringinput=ConsoleReadLine();if(input=="exit")break;newclientSend(EncodingASCIIGetBytes(input));data=newbyte[1024];recv=newclientReceive(data);stringdata=EncodingASCIIGetString(data,0,recv);ConsoleWriteLine(stringdata);}ConsoleWriteLine("disconnectfromsercer");newclientShutdown(SocketShutdownBoth);newclientClose();}}}
以上就是关于谁会java swing窗口自定义全部的内容,包括:谁会java swing窗口自定义、C#网络socket编程,可以传输,客户端退出后,主机不停的接收0、帮忙写一个C#例子:两个进程间通信的问题.等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)