TCP/IP/Socket

来源:互联网 发布:mac不能新建文件夹 编辑:程序博客网 时间:2024/06/07 01:19

socket连接模型

服务端:ServeSocket(“端口”);

ServerSocket.accept();//阻塞

//等待客服端的请求

Socket//得到

OutputStream;

InputStream;

Socket.close;

客服端:Socket(“服务器Ip”,"端口");

OutputStream;

InputStream;

Socket.close;

测试代码:服务端:

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

public class Serve {
/**
* @param args
*/
public static void main(String[] args) {
new Serve().start();
}

public void start(){
ServerSocket ss =null;//定义Serversocket
Socket s  =null;//定义Socket
PrintWriter writer=null;
int port=8888;
String str="[%s:%s]";
try {
//ServeSocket
ss = new ServerSocket(port);
System.out.println("服务器启动了。。");
while(true){
//阻塞
s = ss.accept();
//socket
//拿流
System.out.println("客服端连接上了");
System.out.println(String.format(str, s.getInetAddress().getHostAddress(),s.getPort()));
writer = new PrintWriter(s.getOutputStream());//拿到写入流
//writer.write("hello\r\n");
writer.println("hello");
writer.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(writer!=null)
writer.close();
if(s!=null)
s.close();
if(ss!=null)
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


}


客户端代码:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.logging.Logger;




public class Client {


/**
* @param args
*/
public static void main(String[] args) {
new Client().start();
}

public void start(){
Socket s = null;
BufferedReader br=null;
try{
s = new Socket("192.168.61.71", 8888);
System.out.println("客服端启动了");
br = new BufferedReader(new InputStreamReader(s.getInputStream()));

System.out.println(br.readLine());
}catch(Exception e){

} finally{
if(br!=null)
try {
br.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(s!=null)
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


}


最后还要注意的是PrintWriter的write方法和println方法

查阅api:/**
     * Terminates the current line by writing the line separator string.  The
     * line separator string is defined by the system property
     * <code>line.separator</code>, and is not necessarily a single newline
     * character (<code>'\n'</code>).
     */
    public void println() {
        newLine();
    }


/******************************/

 private void newLine() {
        try {
            synchronized (lock) {
                ensureOpen();
                out.write(lineSeparator);//================这里是一个区别
                if (autoFlush)
                    out.flush();//=======================这里是一个区别
            }
        }


 public PrintWriter(Writer out,
                       boolean autoFlush) {
        super(out);
        this.out = out;
        this.autoFlush = autoFlush;
        lineSeparator = java.security.AccessController.doPrivileged(
            new sun.security.action.GetPropertyAction("line.separator"));
    }

0 0