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"));
}
- tcp ip 通信socket
- socket by tcp/ip
- Socket TCP/IP Sample
- TCP/IP socket.c
- TCP/IP Socket 笔记
- TCP/IP SOCKET HTTP
- TCP/IP SOCKET HTTP
- tcp/ip socket programming
- tcp/ip socket http
- socket --------tcp/ip
- Http, socket tcp/ip
- socket 与tcp/ip
- tcp ip 通信socket
- TCP/IP、Http、Socket
- TCP/IP SOCKET HTTP
- TCP/IP与Socket
- socket和tcp/ip
- TCP/IP socket编程
- LRU缓存淘汰算法
- Java中Runnabale和Thread的区别
- 高次方的尾数
- Java线上排查总结
- iOS开发 -- Objective-c 中 nil, Nil, NULL和NSNull的区别
- TCP/IP/Socket
- C++ 恢复访问权限示例
- 关于“不能实例化抽象类“错误的解决办法
- C 语言之折半查找
- Prototype模式
- PyCharm 支持中文和代理方法
- DP的一些注意点以及题目
- 大数阶乘
- HDOJ题目分类