方法声明 | 功能描述 |
int getPort() | 该方法返回一个int类型对象,该对象是Socket对象与服务器端连接的端口号 |
InetAddress getLocalAddress() | 该方法用于获取Socket对象绑定的本地IP地址,并将IP地址封装成InetAddress类型的对象返回 |
void close() | 该方法用于关闭Socket连接,结束本次通信。在关闭socket之前,应将与socket相关的所有的输入/输出流全部关闭,这是因为一个良好的程序应该在执行完毕时释放所有的资源 |
InputStream getInputStream() | 该方法返回一个InputStream类型的输入流对象,如果该对象是由服务器端的Socket返回,就用于读取客户端发送的数据,反之,用于读取服务器端发送的数据 |
OutputStream getOutputStream() | 该方法返回一个OutputStream类型的输出流对象,如果该对象是由服务器端的Socket返回,就用于向客户端发送数据,反之,用于向服务器端发送数据 |
package com.itheima_04;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
/*
* 使用TCP协议发送数据
创建发送端Socket对象(创建连接)
获取输出流对象
发送数据
释放资源
Socket(InetAddress address, int port)
Exception in thread "main" java.net.ConnectException: Connection refused: connect
*/
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建发送端Socket对象(创建连接)
Socket s = new Socket(InetAddress.getByName("itheima"),10086);
//获取输出流对象
OutputStream os = s.getOutputStream();
//发送数据
String str = "hello tcp,im comming!!!";
os.write(str.getBytes());
//释放资源
//os.close();
s.close();
}
}
package com.itheima_04;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/*
* 使用TCP协议接收数据
创建接收端Socket对象
监听(阻塞)
获取输入流对象
获取数据
输出数据
释放资源
ServerSocket:接收端,服务端Socket
ServerSocket(int port)
Socket accept()
*/
public class ServerDemo {
public static void main(String[] args) throws IOException {
//创建接收端Socket对象
ServerSocket ss = new ServerSocket(10086);
//监听(阻塞)
Socket s = ss.accept();
//获取输入流对象
InputStream is = s.getInputStream();
//获取数据
byte[] bys = new byte[1024];
int len;//用于存储读到的字节个数
len = is.read(bys);
//输出数据
InetAddress address = s.getInetAddress();
System.out.println("client ---> " + address.getHostName());
System.out.println(new String(bys,0,len));
//释放资源
s.close();
//ss.close();
}
}
package com.itheima_05;
package com.itheima_05;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
//创建服务端Socket对象
ServerSocket ss = new ServerSocket(10010);
//监听
Socket s = ss.accept();
//获取输入流对象
InputStream is = s.getInputStream();
//获取数据
byte[] bys = new byte[1024];
int len;//用于存储读取到的字节个数
len = is.read(bys);
String str = new String(bys,0,len);
//输出数据
System.out.println(str);
//转换数据
String upperStr = str.toUpperCase();
//获取输出流对象
OutputStream os = s.getOutputStream();
//返回数据(发出数据)
os.write(upperStr.getBytes());
//释放资源
s.close();
//ss.close();//服务端一般不关闭
}
}
package com.itheima_06;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/*
* 模拟用户登录
*/
public class ClientTest {
public static void main(String[] args) throws IOException {
//创建客户端Socket对象
//Socket s = new Socket(InetAddress.getByName("itheima"),8888);
Socket s = new Socket("itheima",8888);
//获取用户名和密码
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入用户名:");
String username = br.readLine();
System.out.println("请输入密码:");
String password = br.readLine();
//获取输出流对象
//BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
//写出数据
out.println(username);
out.println(password);
//获取输入流对象
BufferedReader serverBr = new BufferedReader(new InputStreamReader(s.getInputStream()));
//获取服务器返回的数据
String result = serverBr.readLine();
System.out.println(result);
//释放资源
s.close();
}
}
package com.itheima_06;
package com.itheima_07;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/*
* 模拟用户登录改写(面向对象版本)
*/
public class ClientTest {
public static void main(String[] args) throws IOException {
//创建客户端Socket对象
Socket s = new Socket("itheima",8888);
//获取用户名和密码
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入用户名:");
String username = br.readLine();
System.out.println("请输入密码:");
String password = br.readLine();
//获取输出流对象
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
//写出数据
out.println(username);
out.println(password);
//获取输入流对象
BufferedReader serverBr = new BufferedReader(new InputStreamReader(s.getInputStream()));
//获取服务器返回的数据
String result = serverBr.readLine();
System.out.println(result);
//释放资源
s.close();
}
}
package com.itheima_07;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
public class ServerTest {
public static void main(String[] args) throws IOException {
//创建服务器端Socket对象
ServerSocket ss = new ServerSocket(8888);
//监听
Socket s = ss.accept();
//获取输入流对象
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//获取用户名和密码
String username = br.readLine();
String password = br.readLine();
//判断用户名和密码是否正确
boolean flag = false;
/*if("itheima".equals(username) && "123456".equals(password)) {
flag = true;
}*/
List<User> users = UserDB.getUsers();
User user = new User(username,password);
if(users.contains(user)) {
//匹配成功
flag = true;
}
//获取输出流对象
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
//返回判断信息
if(flag) {
out.println("登陆成功");
}
else {
out.println("登陆失败");
}
//释放资源
s.close();
//ss.close();//服务器一般不关闭
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |