——Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ——
服务端:ServerSocket ss=new ServerSocket(10010);
获取Socket对象 Socket s=ss.accept();
通过Socket对象获取输入输出流发送消息
客服端:Socket s=new Socket(“localhost”,10010);
通过Socket对象获取输入输出流发送消息
一
package com.itheima.cn;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* @author myluo
*
*
*/
public class TCPSocket {
/**
* tcp协议发送数据
* 1:创建客服端对象 Socket ss=new Socket("ip","端口");
*
* @param args
*/
public static void main(String[] args) {
try {
//创建Socket对象
Socket ss=new Socket("localhost",10006);
//获取输出流
OutputStream out=ss.getOutputStream();
//写入
out.write("你好,我是xxx".getBytes());
//关闭资源
ss.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.itheima.cn;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author myluo
*
*/
public class TCPServer {
/**
* 服务端
* 1:创建ServerSocket("端口号")对象
*
* @param args
*/
public static void main(String[] args) {
try {
//创建ServerSocket对象
ServerSocket ss=new ServerSocket(10006);
//获取客服端的Socket对象
Socket s=ss.accept();
//用客户端对象调用自己的读取流,读取数据
InputStream in=s.getInputStream();
//获取客户端 ip
System.out.println("ip=="+s.getInetAddress().getHostAddress());
//读取流
byte [] by=new byte[1024];
int len=0;
while((len=in.read(by))!=-1){
System.out.println(new String (by,0,len));
}
//关闭客服端
s.close();
//关闭服务器
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
二
package com.itheima.cn;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author myluo
*
*/
public class TCPServer1 {
//创建 服务器端
public static void main(String[] args) {
try {
ServerSocket ss=new ServerSocket(10007);
//获取客服端对象
Socket s=ss.accept();
System.out.println("ip=="+s.getInetAddress().getHostAddress());
//获取读取流
InputStream in=s.getInputStream();
//读取数据
byte [] by=new byte[1024];
int len=in.read(by);
/* while((len=in.read(by))!=-1){
}*/
System.out.println("来自客服端的消息:"+new String(by,0,len));
//获取写入流
OutputStream out=s.getOutputStream();
out.write("你好我是服务器端".getBytes());
//关闭资源
s.close();
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.itheima.cn;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* @author myluo
*
*/
public class TCPSocket1 {
/**
* 服务器的信息的发送和接收
* @param args
*/
public static void main(String[] args) {
Socket s=null;
try {
//创建 Socket对象
s=new Socket("localhost",10007);
//获取 写入 流
OutputStream out=s.getOutputStream();
//写入数据
out.write("你好,我是客户端".getBytes());
//获取 服务器端传来的数据
InputStream in=s.getInputStream();
byte [] by=new byte[1024];
int len=in.read(by);
/*while((len=in.read(by))!=-1){
System.out.println("来至服务器的消息:"+new String(by,0,len));
}*/
System.out.println("来至服务器的消息:"+new String(by,0,len));
//关闭 客服端
s.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
三
package com.itheima.cn;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author myluo
*
* ServerSocket服务端(字母大写返回客户端)
*
*
*/
public class TCPServer3 {
public static void main(String[] args) {
//创建服务端
try {
ServerSocket ss=new ServerSocket(10008);
//创建Socket对象
Socket s=ss.accept();
//创建 读取流
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
//创建 写入流
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String len=null;
while((len=br.readLine())!=null){
System.out.println("客服端:"+len);
bw.write(len.toUpperCase());
bw.newLine();
bw.flush();
}
//关闭资源
s.close();
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.itheima.cn;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* @author myluo
*
* 案例三演示的重点是,readLine(),这种阻塞式方法,在没有遇到回车键时,他会一直阻塞。。
*
* 需求:客服端 键盘录入 小写字母 服务端返回 大写字母
*/
public class TCPSocket3 {
public static void main(String[] args) {
try {
Socket s=new Socket("localhost",10008);
//键盘录入
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//写入服务器的 流
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//读取服务器返回的流
BufferedReader br2=new BufferedReader(new InputStreamReader(s.getInputStream()));
String len=null;
while((len=br.readLine())!=null){
if("over".equals(len)){
break;
}
//把键盘读到写入流中
bw.write(len);
bw.newLine();
bw.flush();
//读取服务器返回的值
String str= br2.readLine();
System.out.println("服务器:"+str);
}
//关闭资源
br.close();//关闭键盘输入流
s.close(); //关闭Socket服务
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}