欢迎来到天天文库
浏览记录
ID:30286770
大小:344.00 KB
页数:24页
时间:2018-12-28
《tcpudp客户服务端实现大小写及socket编程实现聊天程序》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、.1.TCP实现大小写转换代码如下importjava.io.*;importjava.net.*;classTCPServer{publicstaticvoidmain(Stringargv[])throwsException{StringClientSentence;StringcapitalizedSentence;@SuppressWarnings("resource")ServerSocketwelcomeSocket=newServerSocket(8998);while(true){SocketconnectionSocket=welco
2、meSocket.accept();BufferedReaderinfromClient=newBufferedReader(newInputStreamReader(connectionSocket.getInputStream()));DataOutputStreamoutToClient=newDataOutputStream(connectionSocket.getOutputStream());ClientSentence=infromClient.readLine();capitalizedSentence=ClientSentence.t
3、oUpperCase()+'';outToClient.writeBytes(capitalizedSentence);}}} TCP服务器代码: importjava.io.*;importjava.net.*;classTCPClient{publicstaticvoidmain(Stringargv[])throwsException{Stringsentence;StringmodifiedSentence;BufferedReaderinfromUser=newBufferedReader(newInputStreamReader(Sys
4、tem.in));SocketclientSocket=newSocket("127.0.0.1",8998);DataOutputStreamoutToServer=newDataOutputStream(clientSocket.getOutputStream());页.BufferedReaderinfromServer=newBufferedReader(newInputStreamReader(clientSocket.getInputStream()));sentence=infromUser.readLine();outToServer.
5、writeBytes(sentence+'');modifiedSentence=infromServer.readLine();System.out.println("FROMSERVER:"+modifiedSentence);clientSocket.close();}}运行结果截图:1.UDP实现大小写转换页.UDPServer短代码:importjava.net.*;importjava.io.*;publicclassServer{publicstaticvoidmain(Stringargs[])throwsSocketExcepti
6、on,IOException{DatagramSocketsocket;DatagramPacketpacket;byte[]buf=newbyte[256];intport;InetAddressadd;socket=newDatagramSocket(8888);packet=newDatagramPacket(buf,buf.length);booleanb=true;while(b){socket.receive(packet);Strings=newString(packet.getData()).trim();System.out.prin
7、tln("服务器接受数据为:"+s);//Stringstr=s-('a'-'A');Stringstr=s.toUpperCase();s=String.valueOf(str);buf=s.getBytes();packet=newDatagramPacket(buf,buf.length,packet.getAddress(),packet.getPort());socket.send(packet);}socket.close();}}UDP客户端代码:importjava.net.*;importjava.io.*;publicclassCl
8、ient{publicstaticvoidmain(Stringargs[])throwsSo
此文档下载收益归作者所有