JAVA网络编程
大家好,我刚开始学习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 ");
}
}
}
------解决方案--------------------你随便找一本书看看应该就明白了啊
try{
ServerSocket socketObj = null;
try{
socketObj = new ServerSocket(4700);//建立服务器端套接字
}catch(Exception e){