TCP协议:面向连接的,效率较低,需要等待回应后才能继续发送数据。类似于现实中的打电话,当你在向他人打电话时,在播出号码后你都必须等待电话另一边的回应,而在这段时间里,除了等待回应不能做任何的事情。
1.基于单线程,只能有一个客户端向服务器发送请求。
第一步:客户端发送数据给服务器端;
第二步:服务器端接受来自客户端的信息;
第三步:服务器端对客户端做出回应;
第四步:客户端接受接受服务器端的回应。
参考的代码如下:
服务器端:
>package com.geminno.text15;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer {
public static void main(String[] args) {
//创建服务器端套接字
ServerSocket server =null;
DataInputStream dis=null;
DataOutputStream dos=null;
Socket client=null;
try {
server=new ServerSocket(8888);
//设置监听
client=server.accept();
//获取客户端的输入流,用来读取客户端发来的数据
dis=new DataInputStream(client.getInputStream());
dos=new DataOutputStream(client.getOutputStream());
while(true){
String s=dis.readUTF();
System.out.println("接受来自客户端的信息--->"+s);
dos.writeUTF(s);
}
} catch (IOException e) {
System.out.println("");
}finally{
try {
dis.close();
} catch (IOException e) {
e.getMessage();
}finally{
try {
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
客户端:
> package com.geminno.text15;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class MyClient {
public static void main(String[] args) {
Socket client=null;
DataOutputStream oos=null;
DataInputStream dis=null;
try {
//创建客户端套接字
client=new Socket("127.0.0.1",8888);
oos=new DataOutputStream(client.getOutputStream());
dis=new DataInputStream(client.getInputStream());
for (int i = 0; i < 10; i++) {
oos.writeUTF("hello--->"+i);
System.out.println("回应:"+dis.readUTF());
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(oos!=null){
try {
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
2、基于多线程。服务器端将数据交给线程来处理,这样服务器就能同时处理多条请求,提高效率。
服务器端:
>package com.geminno.text15.tcp1;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer {
public static void main(String[] args) {
ServerSocket server=null;//服务器端套接字
Socket client=null;
try {
server=new ServerSocket(8080);
//等待客户端链接
while(true){
client=server.accept();
//交给线程处理
ServerThread st=new ServerThread(client);
new Thread(st).start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
线程:
> package com.geminno.text15.tcp1;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class ServerThread implements Runnable {
Socket client;
public ServerThread(Socket client) {
super();
this.client = client;
}
@Override
public void run() {
DataInputStream dis = null;
DataOutputStream dos = null;
String name = Thread.currentThread().getName();
try {
dis = new DataInputStream(client.getInputStream());
dos = new DataOutputStream(client.getOutputStream());
while (true) {
String s = dis.readUTF();
System.out.println("接受来自客户端" + name + "的信息" + s);
dos.writeUTF(s.length() + "");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
dis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
客户端:这里的写法与单线程的没有区别。
> package com.geminno.text15.tcp1;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
public class MyClient {
public static void main(String[] args) {
Socket client = null;//客户端套接字
DataInputStream dis = null;//接受来自服务器端的信息
DataOutputStream dos = null;//发送数据给服务器端
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
try {
client = new Socket("127.0.0.1", 8080);
//127.0.0.1:服务器的IP地址 8080:要连接的服务器的端口号
dos = new DataOutputStream(client.getOutputStream());
dis = new DataInputStream(client.getInputStream());
while (true) {
System.out.print("请输入信息:");
String s = sc.nextLine();
dos.writeUTF(s);
String str = dis.readUTF();
System.out.println("接受 来自服务器端的回应---->"+str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
dis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}