📄 tcpservera.java
字号:
package day15;
import java.net.*;
import java.io.*;
import java.util.*;
/**
*
* @author Gunbuster
*
*/
public class TCPServerA {
public static void main(String[] args) {
ServerSocket ss=null;
Socket s=null;
PrintStream ps=null;
String sta=null;
String stb=null;
try {
ss=new ServerSocket(9999);
while(true){
System.out.println("服务器已启动,在9999端口:");
s=ss.accept();//等 直到有客户端的请求
System.out.println("得到"+s.getInetAddress()+"的连接请求......");
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
sta=br.readLine();
System.out.println("从客户端读到的数据为:");
System.out.println(sta);
stb=sta.toUpperCase();//将客户端传过来的数据转换成大写;
System.out.println("在服务器处理之后的字符串为:");
System.out.println(stb);
PrintWriter pw=new PrintWriter(s.getOutputStream());
pw.println(stb);
pw.flush();//将处理好的 数据写到客户端
// ps=new PrintStream(s.getOutputStream());//包装成PrintStream
// ps.println(new Date());//返回对象字符串
// ps.flush();
System.out.println("已经向客户端发送数据!");
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(ps!=null)ps.close();
if(s!=null)
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
if(ss!=null){
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -