欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Socket

程序员文章站 2022-07-11 08:26:57
...

通信过程
Socket
协议TCP
Socket
1.transfer control protocal,传输控制协议
2:必须先建立连接
3:安全协议
4:有序

UDP与TCP
Socket
应用场合
Socket

1:进程间通信
2:API
ServerSocket //服务器端套接字
Socket //套接字
3:协议
//数据格式:
TCP //传输控制协议,面向连接,基于流的
UDP //用户数据包协议,无连接的
IP //Internet procatl,
FTP //file transfer prorocal
SMTP //simple main transfer protocal
http //超文本传输协议
4:
单工:有去无回
半双工:不能同时进行收发
全双工:能同时进行收发

一:实现连接

Socket

package fanyongjun.Socket;
import java.net.Socket;
public class SocketDemo {
 public static void main(String[] args) throws Exception {
  Socket sock=new Socket("localhost",9999);
  System.out.println("local:"+sock.getLocalPort());
  System.out.println("port:"+sock.getPort());
  
while(true) {
 Thread.sleep(1000);
}
 }
}

ServerSocket

package fanyongjun.Socket;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerSocketDemo {
public static void main(String[] args) throws Exception {
 //创建ServerSocket对象
 ServerSocket ss=new ServerSocket(9999);
 //System.out.println(ss.getLocalPort());
 //接收
 while(true) {
 Socket sock=ss.accept();
 System.out.println("localport:"+sock.getLocalPort());
 System.out.println("port:"+sock.getPort());
 } 
}
}

在socket中简单通信

MySocket

public class MyScoket {
public static void main(String[] args) throws Exception, Exception {
 Socket socket =new Socket("localhost",8888);
 OutputStream os=socket.getOutputStream();
 InputStream is=socket.getInputStream();
 //主动向服务器发送一条消息
 os.write("hello world".getBytes());
 byte []buffer=new byte[1024];  
 int len=-1;
 while((len=is.read(buffer))!=-1) {
  String msg=new String(buffer,0,len);
  System.out.println("收到回执:"+msg);
  os.write(("from client"+msg).getBytes());
 Thread.sleep(500);
 }
 os.close();
 socket.close();
}
}

MyServerSocket

package fanyongjun.socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
 * 服务器端
 * @author Frenzy Fan
 *
 */
public class MyServerSocket {
public static void main(String[] args) throws Exception {
 //创建服务器 Socket
 ServerSocket ss=new ServerSocket(8888);
 //接受连接请求
 Socket  s=ss.accept();
 System.out.println("有人连进来了");
 //得到输入流,读取客户端的请求数据
 InputStream is=s.getInputStream();
 OutputStream os=s.getOutputStream();
 byte []bytes=new byte[1024]; 
 int len=-1;
 while((len=is.read(bytes))!=-1) {
 System.out.println("收到消息"+new String(bytes,0,len));
 String msg="From sever:"+new String(bytes,0,len);
 os.write(msg.getBytes());
}
System.out.println("流关闭了!");
is.close();
s.close();
ss.close();
}
}

在socket中1-1通信

MySocket

package fanyongjun.socket2;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
public class Myscoket {
public static void main(String[] args) throws Exception, Exception {
 //创建socket对象
 Socket socket =new Socket("localhost",8888);
 OutputStream os=socket.getOutputStream();
 //缓冲区服务器
 BufferedReader bis=new BufferedReader(new InputStreamReader(System.in));
 String line=null;
 while(true) {
  line=bis.readLine();
  if(line!=null&&"exit".equals(line)) {
   break ;
  }
  if(line!=null) {
   os.write(line.getBytes());
  }
 }
 os.close();
 socket.close();
}
}

MyServerSocket

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
 * 服务器端
 * @author Frenzy Fan
 *
 */
public class MyServerSocket {
public static void main(String[] args) throws Exception {
 //创建服务器 Socket
 ServerSocket ss=new ServerSocket(8888);
 //接受连接请求
 Socket  s=ss.accept();
 System.out.println("有人连进来了");
 //得到输入流,读取客户端的请求数据
 InputStream is=s.getInputStream();
 byte []bytes=new byte[1024]; 
 int len=-1;
 while((len=is.read(bytes))!=-1) {
 System.out.println("收到消息"+new String(bytes,0,len));
 String msg="From sever:"+new String(bytes,0,len);
}
System.out.println("流关闭了!");
is.close();
s.close();
ss.close();
}
}

socket中多对一通信

MySocket

package fanyongjun.socket3.copy;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
public class Myscoket {
public static void main(String[] args) throws Exception, Exception {
 //创建socket对象
 Socket socket =new Socket("localhost",8888);
 OutputStream os=socket.getOutputStream();
 //缓冲区服务器
 BufferedReader bis=new BufferedReader(new InputStreamReader(System.in));
 String line=null;
 while(true) {
  line=bis.readLine();
  if(line!=null&&"exit".equals(line)) {
   break ;
  }
  if(line!=null) {
   os.write(line.getBytes());
  }
 }
 os.close();
 socket.close();
}
}

MyServerSocket

package fanyongjun.socket3.copy;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
 * 服务器端
 * @author Frenzy Fan
 *
 */
public class MyServerSocket {
public static void main(String[] args) throws Exception {
 //创建服务器 Socket
 ServerSocket ss=new ServerSocket(8888);
 System.out.println("服务器启动了");
 //接受连接请求
 while(true) {
 Socket  s=ss.accept();
 new SessionThread(s).start();;
 }
 }
}

SessionThread

package fanyongjun.socket3.copy;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class SessionThread extends Thread {
private Socket socket;
private String descStr;
public SessionThread(Socket socket) {
 this.socket=socket;
}
public void run() {
 try {
  //返回远端的ip地址
  InetAddress addr=socket.getInetAddress();
  //远程主机名
  String hostname=addr.getHostName();
  //返回远端的端口号
  int port= socket.getPort();
  descStr = "{"+hostname+":"+port+"}";
  System.out.println(descStr+"上线了");
  OutputStream os=socket.getOutputStream();
  //in
  InputStream is=socket.getInputStream();
  //
  byte[]buffer=new byte[1024];
  int len=-1;
  while((len=is.read(buffer))!=-1){
   System.out.println(descStr+"说:"+new String(buffer,0,len));
   //System.out.println();
   //把消息回传给客户端
   //os.write(buffer,0,len);
  }
System.out.println(descStr+"下线了");
 } catch (IOException e) {
  System.out.println(descStr+"异常下线了");
  e.printStackTrace();
 }
}
}
相关标签: java