主要代码如下:clientimport java.net.*;
import java.io.*;
public class MyClient {
public static void main(String[] args) {
try{
Socket s = new Socket("localhost" , 8000) ;
OutputStream os = s.getOutputStream() ;
OutputStreamWriter osw = new OutputStreamWriter(os) ;
PrintWriter pw = new PrintWriter(osw,true) ;
pw.println("王洋") ;
InputStream is = s.getInputStream() ;
InputStreamReader isr = new InputStreamReader(is) ;
BufferedReader br = new BufferedReader(isr) ;
String mess = br.readLine() ;
System.out.println(mess) ;
}catch(Exception e){}
}
}
server:
import java.net.*;
import java.io.*;
public class MyServer {
public static void main(String[] args) {
try{
ServerSocket ss = new ServerSocket(8000) ;
System.out.println("监听前......") ;
Socket s = ss.accept() ;
InputStream is = s.getInputStream() ;
InputStreamReader isr = new InputStreamReader(is) ;
BufferedReader br = new BufferedReader(isr) ;
String name = br.readLine() ;
System.out.println(name) ;
OutputStream os = s.getOutputStream() ;
OutputStreamWriter osw = new OutputStreamWriter(os) ;
PrintWriter pw = new PrintWriter(osw , true) ;
pw.println("欢迎"+name) ;
}catch(Exception e){}
}
}
|
|