------------------------------------------------------------------------------------------------------
在TCP/IP协议中IP层主要负责网络主机的定位,数据传输的路由,由IP地址可以唯一地确定Internet上的一台主机。
一个Socket由一个IP地址和一个端口号唯一确定。
-------------------------------------服务器端------------------------------------------------------
public class SocketManager {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) {
SocketManager manager = new SocketManager();
manager.doListen();
}
public void doListen() {
ServerSocket server;
try {
server = new ServerSocket(3030);
while (true) {
Socket client = server.accept();
/*第一种开线程的方法*/ new Thread(new SSocket(client)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
class SSocket implements Runnable {
Socket client;
public SSocket(Socket client) {
this.client = client;
}
public void run() {
InputStream inputStream =null;
PrintStream pw =null;
try {
//接收数据
inputStream = client.getInputStream();
pw = new PrintStream(this.client.getOutputStream(), true);
byte[] buff = new byte[128];
int n = -1;
while((n = inputStream.read(buff)) != -1)
{
String strs=new String(buff,"utf-8");//
//发送数据
char[] send_buf2=new char[32];
send_buf2=strs.toCharArray();
pw.print(send_buf2);
pw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
pw.close();
}
--------------------------------------------------------------------------
/*第二种开线程的方法*/
try {
final ServerSocket serverSocket = new ServerSocket(3030);
while(true)
{
final Socket socket = serverSocket.accept();
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
---------------------------------------------------------------------------
----------------------------------------客户端------------------------------------------------------
Socket socket = null;
OutputStream doc =null;
socket = new Socket(IP, 3030);
byte[] sendBytes= content.getBytes("UTF-8");
doc= socket.getOutputStream();
doc.write(sendBytes);
catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流和连接
try {
doc.close();
} catch (Exception e1) {
e1.printStackTrace();
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
}
}
}
---------------------------------------------------------------------------------------------------
在TCP/IP协议中IP层主要负责网络主机的定位,数据传输的路由,由IP地址可以唯一地确定Internet上的一台主机。
一个Socket由一个IP地址和一个端口号唯一确定。
-------------------------------------服务器端------------------------------------------------------
public class SocketManager {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) {
SocketManager manager = new SocketManager();
manager.doListen();
}
public void doListen() {
ServerSocket server;
try {
server = new ServerSocket(3030);
while (true) {
Socket client = server.accept();
/*第一种开线程的方法*/ new Thread(new SSocket(client)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
class SSocket implements Runnable {
Socket client;
public SSocket(Socket client) {
this.client = client;
}
public void run() {
InputStream inputStream =null;
PrintStream pw =null;
try {
//接收数据
inputStream = client.getInputStream();
pw = new PrintStream(this.client.getOutputStream(), true);
byte[] buff = new byte[128];
int n = -1;
while((n = inputStream.read(buff)) != -1)
{
String strs=new String(buff,"utf-8");//
//发送数据
char[] send_buf2=new char[32];
send_buf2=strs.toCharArray();
pw.print(send_buf2);
pw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
pw.close();
}
--------------------------------------------------------------------------
/*第二种开线程的方法*/
try {
final ServerSocket serverSocket = new ServerSocket(3030);
while(true)
{
final Socket socket = serverSocket.accept();
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
---------------------------------------------------------------------------
----------------------------------------客户端------------------------------------------------------
Socket socket = null;
OutputStream doc =null;
String str=null;
String content="aaaaaaaa";
socket = new Socket(IP, 3030);
byte[] sendBytes= content.getBytes("UTF-8");
doc= socket.getOutputStream();
doc.write(sendBytes);
catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流和连接
try {
doc.close();
} catch (Exception e1) {
e1.printStackTrace();
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
}
}
}
---------------------------------------------------------------------------------------------------