tcpserver.java
来自「java写的一个Tcp socket 一个client和server」· Java 代码 · 共 64 行
JAVA
64 行
package com.demo.struts.else_test;
//服务器端源程序
import java.io.*;
import java.net.*;
public class TcpServer
{
@SuppressWarnings("deprecation")
public static void main(String args[]) throws IOException{
ServerSocket svrsoc = null;
Socket soc = null;
InputStream is = null;
OutputStream os = null;
//DataInputStream 为InputStream的子类
DataInputStream in = null;
//PrintStream为OutputStream的子类
PrintStream out = null;
try{
svrsoc = new ServerSocket(8000);
soc = svrsoc.accept();
is = soc.getInputStream();
in = new DataInputStream(is);
os = soc.getOutputStream();
out = new PrintStream(os);
InetAddress clientIP = soc.getInetAddress();
System.out.println("Client`s IP address:" + clientIP);
int port ;
port = soc.getPort();
System.out.println("Client`s port :" + port);
out.println("Welcome! Happy newyear!");
String str = in.readLine();
while(! str.equals("quit")){
System.out.println("Client said:" + str);
str = in.readLine();
}
System.out.println("Client want to leave.");
}catch(Exception e){
System.out.println("Error:" + e);
}
finally{
is.close();
os.close();
soc.close();
System.exit(0);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?