Java Socket 入门1

由程序逻辑可以看到 这是一个 客户端和服务端一对一聊天的程序  首先由服务端说第一句话然后对话才开始 

且只能客户端一行话  服务端再一行话 这样往复进行  客户端若想不等服务端回应继续说话是不行的

服务端

package rakus;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.PrintWriter;

import java.net.ServerSocket;

import java.net.Socket;
public class MyServer {
    public static void main(String[] args) {
        try {
            ServerSocket ss;
            ss = new ServerSocket(8888);
            //wait for client to link then there will be a link between server and client
            Socket sk = ss.accept();
            System.out.println("a connection set up");
            //server has to send msg to client and get msg from client
            InputStream is=sk.getInputStream();
            OutputStream os=sk.getOutputStream();
            //BufferedReader对InputStreamReader作封装 以便于读一行数据
            BufferedReader br=new BufferedReader(new InputStreamReader(is));
            PrintWriter pw=new PrintWriter(os);
            //get msg from console and send
            BufferedReader brConsole=new BufferedReader(new InputStreamReader(System.in));
            String msg=null;
            String str = null;
            while(1==1){
                //print
                msg=brConsole.readLine();
                pw.println  ("server :  "+msg);
                //注意这里不能使用print  因为对方是使用readLine()来接收数据的 
                //没有ln的话总是认为一行话没有结束
                pw.flush();//pw可以使用  new PrintWriter(os.true)来创建  true表示autoFlush//那么就不用我手动Flush了
                //read str from scoket
                str = br.readLine();
                System.out.println(str);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
 

 

客户端

package rakus;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
public class MyClient {
    public static void main(String[] args) {
        try {
            // link server via ip and port
            // InetAddress.getLocalHost() 局域网中得到本机IP地址
            Socket sk = new Socket("127.0.0.1", 8888);
            // inputSteam is used for get msg from server
            // outputStream is for send msg to server
            InputStream is = sk.getInputStream();
            OutputStream os = sk.getOutputStream();
            //BufferedReader往往用来读一行数据    br是用于读网络中的一行数据
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            //pw用于把数据写入到网络中
            PrintWriter pw = new PrintWriter(os);
            //读取控制台的文字
            BufferedReader brConsole = new BufferedReader(new InputStreamReader(
                    System.in));
            // get a msg from server
            String str = null;
            String msg=null;//msg是发往客户端的消息
            while(1==1){
                //read
                str = br.readLine();
                System.out.println(str);
                // send msg to server
                msg = brConsole.readLine();
                pw.println("client : "+msg);
                pw.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

 

 

posted @ 2013-10-15 14:27  cart55free99  阅读(207)  评论(0编辑  收藏  举报