JAVA网络编程

hcyjsj86 hcyjsj86 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
大家好,我刚开始学习JAVA网络编程,请问哪位高手可以把下面的这个服务器端程序和客户端程序的注释详细的写一下给我啊,谢谢了!

服务器端:
import java.io.*;
import java.net.*;
import java.applet.Applet;
public class ServerSocketTest{
public static void main(String args[]){
try{
ServerSocket socketObj = null;
try{
socketObj = new ServerSocket(4700);
}catch(Exception e){
System.out.println("There is a connetion error");
}
Socket clientObj = null;
try{
clientObj = socketObj.accept();
}catch(Exception e){
System.out.println("Error:"+e);
}
String strLine;
InputStreamReader streamObj1 = new InputStreamReader(clientObj.getInputStream());
BufferedReader buffObj1 = new BufferedReader(streamObj1);
OutputStream outObj= new BufferedOutputStream(clientObj.getOutputStream());
PrintWriter writerObj = new PrintWriter(outObj);
InputStreamReader streamObj2 = new InputStreamReader(System.in);
BufferedReader buffObj2 = new BufferedReader(streamObj2);
System.out.println("Client words:"+buffObj1.readLine());
strLine = buffObj2.readLine();
while(!strLine.equals("Finished"))
{
writerObj.println(strLine);
writerObj.flush();
System.out.println("Server words:"+strLine);
System.out.println("Client words:"+buffObj1.readLine());
strLine = buffObj2.readLine();
}
buffObj1.close();
writerObj.close();
clientObj.close();
socketObj.close();
}catch(Exception e){
System.out.println("Error:"+e);
}
}
}

客户端:
import java.io.*;
import java.net.*;
public class ClientSocketTest{
public static void main(String args[]){
try{
Socket socketObj = new Socket("127.0.0.1",4700);
InputStreamReader instreamObj=new InputStreamReader(System.in);
BufferedReader buffObj1=new BufferedReader(instreamObj);
OutputStream outstreamObj=socketObj.getOutputStream();
PrintWriter writerObj=new PrintWriter(outstreamObj);
InputStream inputObj=socketObj.getInputStream();
InputStreamReader instreamObj2=new InputStreamReader(inputObj);
BufferedReader buffObj2=new BufferedReader(instreamObj2);
String strLine;
strLine = buffObj1.readLine();
while(!strLine.equals("Finished"))
{
writerObj.println(strLine);
writerObj.flush();
System.out.println("Client words:"+strLine);
System.out.println("Server words:"+buffObj2.readLine());
strLine = buffObj1.readLine();
}
writerObj.close();
buffObj2.close();
socketObj.close();
}catch(IOException e){
System.out.println("IO error");
}
}
}
...全文
评论 1
收藏 已收藏
分享
返回顶部
hcyjsj86
Java SE 62,621 成员 307,251 内容