Java基础-服务器的发送和接收

package hanqi.test;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class TestServer1 {

    @SuppressWarnings("resource")
    public static void main(String[] args) throws Exception {
        
        System.out.println("启动服务器监听...");
        ServerSocket ss = new ServerSocket(83);
        
        Socket sc= ss.accept();
        
        //服务器发送
        OutputStream out = sc.getOutputStream();
        
        //构造输出工具类
        PrintWriter pw = new PrintWriter(out);
        pw.println("通过工具类发送的测试语句");
        
        pw.close();
        
        out.close();
        
        sc.close();
        
        System.out.println("发送完毕");
        
    }    
}
package hanqi.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;

public class TestClient1 {

    public static void main(String[] args) throws Exception {
        // TODO 自动生成的方法存根
        
        Socket sc =new Socket("localhost",83);
        
        //接收
        InputStream in = sc.getInputStream();
        
        
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        
        String str = br.readLine();
        
        System.out.println("从服务器接受的数据="+str);
        
    }

}

 

posted @ 2016-09-29 14:11  张好好  阅读(380)  评论(0编辑  收藏  举报