1、套接字发送文字
1.1、 服务端
public class LonginServer {
public static void main(String[] args) {
ServerSocket ss=null;
Socket s=null;
InputStream is=null;
BufferedReader br=null;
try{
//相当于将套接字服务器打开,并且监听8800端口
ss=new ServerSocket (8800);
//获取套接字对象
s=ss.accept();
//通过套接字对象,获取我们的字节输入流
is=s.getInputStream();
//通过字节流获取带缓存的字符流
br =new BufferedReader(new InputStreamReader(is));
String info=null;
while(!((info=br.readLine())==null)){
System.out.println("端口号8800服务器接收到信息:"+info);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
//反向关流
if(br!=null){
try{
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(s!=null){
try{
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(ss!=null){
try{
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
1.2、 客户端
public class LonginClient {
public static void main(String[] args) {
Socket socket=null;
OutputStream os=null;
try{
//新创建一个套接字,参数为地址+端口号
socket=new Socket("192.168.1.211",8800);//本地地址localhost
//获取字节输出流
os= socket.getOutputStream();
//创建一个字符串,让我们好去发送
System.out.println("请问你想向服务器发送什么信息?");
String str="测试!测试!cccccc";
//发送需要发送的文字
os.write(str.getBytes());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if (os!=null){
try{
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket!=null){
try{
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2、套接字发送对象
2.1、 对量实体类
/*
*用户类:用户名和密码
*/
public class User implements Serializable{
private String userName;
private String pwd;
public User(){
}
public User(String userName,String pwd){
this.userName=userName;
this.pwd=pwd;
}
public String getUserName(){
return userName;
}
public void setUserName(String){
this.userName=userName;
}
public String getPwd(){
return pwd;
}
public void setPwd(String pwd){
this.pwd=pwd;
}
}
2.2、 服务端
public class LonginServer02 {
public static void main(String[] args) {
try{
//1.创建一个服务器Socket
ServerSocket serverSocket=new ServerSocket(5000);
//2.使用accppt( )等待客户的通信
Socket socket=serverSocket.accpet();
//3.获得输入流,获得相应的用户请求
InputStream is=socket.getInputStream();
ObjectInputStream ois=new ObjectInputStream(is);
User user=(User)ois.readObject();
System.out.println("我是服务器,接收到信息:"+user.getUserName()+
"--"+user.getPwd());
//给客户端一个响应
String replay="欢迎登陆!";
byte[] replys=reply.getBytes();
OutputStream os=socket.getOutputStream();
os.write(replys);
//4.释放相应资源
os.close();
ois.close();
is.close();
socket.close();
serverSocket.close();
}catch(IOException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
}
2.3、 客户端
public class LonginClient02 {
public static void main(String[] args) {
try{
//1.创建以后个客户端Socket
Socket socket=new Socket("localhost",5000);
//2.输出通过流发送请求
OutputStream os=socket.getOutputStream();
User user=new User("TOM","123456");
ObjectOutputStream oos=new ObjectOutputStream(os);
oos.witeObject(user);
socket.shutdownOutput();
//通过输入流来就接收到服务器给客户端的响应
InputStream is=socket.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String reply;
while((rely=br.readLine())!=null){
System.out.println("服务器的响应"+reply);
}
//3.释放资源
br.close();
is.colse();
os.close();
socket.colse();
}catch(UnKnownHosExcepton e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
}
3、 通过线程形式完成套接字
由于套架子对象每次在客户实例化一个都需要在客户端申请以下
3.1、 对象
public class User01 implements Serializable{
private String loginName;//用户名
private String ped;//用户密码
public User(){
}
public User(String userName,String pwd){
this.loginName=loginName;
this.pwd=pwd;
}
public String getLoginName(){
return loginName;
}
public void setLoginName(String){
this.loginName=loginName;
}
public String getPwd(){
return pwd;
}
public void setPwd(String pwd){
this.pwd=pwd;
}
}
3.2、 服务端
public class LonginServer02 {
public static void main(String[] args) {
try{
//建立个服务器Socket(ServerScoket)指定端口并开始监听
ServerSocket serverSocket=new ServerSocket(5000);
//使用accept()方法等待与客户端触发通信
Socket socket=null;
//监听一直进行中
while(true){
socket=serverSocket.accept();
LoginThread LoginThread=new LoginThread(stoket);
LoginThread.start();
}
}catch(IOException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
}
3.3、 客户端(可以有多个)
public class LonginClient02 {
public static void main(String[] args) {
try{
//建立客户端Socket链接,指定服务器的位置以及端口
Socket socket=new Socket("localhost",5000);
//打开输入输出流
OutputStream os=socket.getOutputStream();
InputStream is=socket.getIntputStream();
//对象序列化
ObjectOutputStream oos=new ObjectOutputStream(os);
//发送客户端登录信息,即向输出流写入信息
User user=new User();
user.setLoginName("Tom");
user.setPed("123456");
oos.witeObject(user);
socket.shutdownOutput();
//接收服务器端的想要,即从输入流读取信息
String reply=null;
BufferedReader br=new BufferedReader(new InputStreamReader(is));
while((rely=br.readLine())!=null){
System.out.println("我是客户端,服务器的响应为:"+reply);
}
//关闭资源
oos.close();
is.colse();
os.close();
socket.colse();
}catch(UnKnownHosExcepton e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
}
3.4、 服务端的多线程
public class LoginThread extends Thread{
Socket socket=null;
//每启动一个线程,对应Socket
public LoginThread(Socket socket){
this.socket=socket;
}
//启动线程,即响应客户请求
public void run(){
try{
//打开输入输出流
InputStream is=socket.getInputStream();
OutputStream os=socket.getOutputStream();
//反序列化
ObjectInputStream ois=new OnjectInputStream(is);
//获取客户端信息,即从输入流读取信息
//BufferedRead br=new BufferedReader(new InputStreamReader(ois));
User user=(User)ois.readObject();
if(!(user==null)){
System.out.println("我是服务器,客户登录信息为:"+
user.getLoginName()+","+userPwd());
}
//给客户端一个响应,即向输出流写入信息
String reply="欢迎你,登陆成功!";
os.write(reply.getBytes());
//关闭资源
ois.colse();
os.colse();
is.colse();
socket.close();
}catch(UnKnowHostException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
e.printStackTrace
}
}
}