一、TCP基本步骤
1)服务器端
package com.sxt.tcp;
//----服务器端----
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/*TCP基本步骤
*1、创建服务器套接字
* 2、使用accept方法建立连接
* 3、操作
* 4、释放资源
*/
public class Server00 {
public static void main(String[] args) throws IOException {
//1、创建服务器套接字
ServerSocket server =new ServerSocket(1234);
//2、使用accept方法建立连接
Socket client=server.accept();
//3、操作
DataInputStream dis =new DataInputStream(client.getInputStream());
String msg=dis.readUTF();
System.out.println(msg);
//4、释放资源
dis.close();
client.close();
}
}
2)客户端
package com.sxt.tcp;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
/*客户端
*1、使用Socket()构造方法建立连接
* 2、操作
* 3、释放资源
*/
public class Client00 {
public static void main(String[] args) throws IOException {
//1、使用Socket()构造方法建立连接
Socket client=new Socket("localhost",1234);
//2、操作
DataOutputStream dos=new DataOutputStream(client.getOutputStream());
dos.writeUTF("上海尚学堂");
dos.flush();
//3、释放资源
dos.close();
client.close();
}
}
要点总结:
1、Socket类提供getInputStream和getOutputStream来得到输入流和输出流
2、最后记得关闭流和Socket对象
二、双向输出
1)客户端
package com.sxt.tcp;
import java.io.*;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
System.out.println("---客户端---");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入用户名:");
String uname=br.readLine();
System.out.println("请输入密码:");
String upwd=br.readLine();
//1、创建流套接字 指定IP地址、端口号
Socket socket=new Socket("localhost",1234);
//2、操作
DataOutputStream dos=new DataOutputStream (socket.getOutputStream());
dos.writeUTF("uname="+uname+"&"+"upwd="+upwd);
dos.flush();
DataInputStream dis=new DataInputStream(socket.getInputStream());
String data=dis.readUTF();
System.out.println(data);
//3、释放资源
dos.close();
socket.close();
}
}
2)服务器端
package com.sxt.tcp;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/* 建立服务器
*1、指定端口,使用ServerSocket创建服务器
*2、使用accept方法建立连接
* 3、操作:输出输入操作
* 4、释放资源
*/
public class Server {
public static void main(String[] args) throws Exception {
System.out.println("---服务器---");
//1、指定端口,使用ServerSocket创建服务器
ServerSocket server=new ServerSocket(1234);
//2、使用accept方法建立连接
Socket socket=server.accept();
//3、操作:输入输出操作
DataInputStream dis=new DataInputStream(socket.getInputStream());
String data=dis.readUTF();
String uname="";
String upwd="";
String[] dataArray=data.split("&");
for(String info: dataArray){
String[] userinfo=info.split("=");
if(userinfo[0].equals("uname")){
System.out.println("你的用户名为"+userinfo[1]);
uname=userinfo[1];
}else if(userinfo[0].equals("upwd")){
System.out.println("你的密码为"+userinfo[1]);
upwd=userinfo[1];
}
}
DataOutputStream dos=new DataOutputStream (socket.getOutputStream());
if(uname.equals("shsxt")&&upwd.equals("laopei")){
dos.writeUTF("登录成功,欢迎回来");
}else{
dos.writeUTF("登录失败");
}
dos.flush();
//4、释放资源
dis.close();
socket.close();
server.close();
}
}
三、文件上传
1)客户端
package com.sxt.tcp;
import java.io.*;
import java.net.Socket;
//客户端
public class FileClient {
public static void main(String[] args) throws Exception {
System.out.println("---客户端---");
//1、创建流套接字 指定IP地址、端口号
Socket socket=new Socket("localhost",8888);
//2、操作
InputStream is=new BufferedInputStream(new FileInputStream("abc.txt"));
OutputStream os=new BufferedOutputStream(socket.getOutputStream());
byte[] flush=new byte[1024];
int len=-1;
while((len=is.read(flush))!=-1){
os.write(flush,0, flush.length);
os.flush();
}
//3、释放资源
os.close();
is.close();
socket.close();
}
}
2)服务器端
package com.sxt.tcp;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class FileServer {
public static void main(String[] args) throws Exception {
ServerSocket server=new ServerSocket(8888);
Socket socket=server.accept();
InputStream is=new BufferedInputStream(socket.getInputStream());
OutputStream os=new BufferedOutputStream(new FileOutputStream("tcp.txt"));
byte[] flush=new byte[1024];
int len=-1;
while((len=is.read(flush))!=-1){
os.write(flush,0, flush.length);
os.flush();
}
os.close();
is.close();
socket.close();
server.close();
}
}
四、多用户登录
?1)客户端
package com.sxt.tcp;
import java.io.*;
import java.net.Socket;
/*建立客户端
*1、创建流套接字 指定IP地址、端口号
* 2、操作:输入输出操作
* 3、释放资源
*/
public class LoginMultiClient {
public static void main(String[] args) throws Exception {
System.out.println("---客户端---");
//1、创建流套接字 指定IP地址、端口号
Socket socket=new Socket("localhost",1234);
new Send(socket).send();
new Receive(socket).receive();
socket.close();
}
static class Send{
private Socket socket;
private DataOutputStream dos;
private BufferedReader br;
private String msg;
public Send(Socket socket) {
br=new BufferedReader(new InputStreamReader(System.in));
this.msg=init();
this.socket = socket;
try {
dos=new DataOutputStream (socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void send(){
try {
dos.writeUTF(msg);
dos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
private String init(){
try {
System.out.println("请输入用户名:");
String uname=br.readLine();
System.out.println("请输入密码:");
String upwd=br.readLine();
return "uname="+uname+"&"+"upwd="+upwd;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
static class Receive {
private DataInputStream dis;
private Socket socket;
public Receive(Socket socket) {
this.socket = socket;
try {
dis = new DataInputStream(socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void receive(){
try {
String data=dis.readUTF();
System.out.println(data);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2)服务器端
package com.sxt.tcp;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class LoginMultiServer {
public static void main(String[] args) throws IOException {
System.out.println("---服务器---");
//1、指定端口,使用ServerSocket创建服务器
ServerSocket server = new ServerSocket(1234);
boolean flag=true;
while (flag) {
Socket socket = server.accept();
new Thread(new Channel(socket)).start();
}
server.close();
//2、使用accept方法建立连接
}
static class Channel implements Runnable{
private DataInputStream dis;
private Socket socket;
private DataOutputStream dos;
public Channel(Socket socket) {
this.socket = socket;
try {
dis=new DataInputStream(socket.getInputStream());
dos=new DataOutputStream (socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
release();
}
}
@Override
public void run() {
System.out.println("一个客户端建立了连接");
String data=receive();
String uname="";
String upwd="";
String[] dataArray=data.split("&");
for(String info: dataArray){
String[] userinfo=info.split("=");
if(userinfo[0].equals("uname")){
System.out.println("你的用户名为"+userinfo[1]);
uname=userinfo[1];
}else if(userinfo[0].equals("upwd")){
System.out.println("你的密码为"+userinfo[1]);
upwd=userinfo[1];
}
}
if(uname.equals("shsxt")&&upwd.equals("laopei")){
send("登录成功,欢迎回来");
}else{
send("登录失败");
}
release();
}
private String receive(){
String data= null;
try {
data = dis.readUTF();
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
private void send(String msg){
try {
dos.writeUTF(msg);
dos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
private void release(){
//4、释放资源
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
问题总结:(细节细节!!!)
在receive方法中一定要注意此方法有返回值,所以要把dis.readUTF()赋值给String msg;否则读到的内容为空,结果一定会报错。(这个错误我找了几个小时!!!)
|