TCP只分客户端和服务端,而且必须先有服务端
示例1:
class TcpClient {
public static void main(String[] args) throws Exception, IOException {
//1.创建客户端的socket服务,指定主机和端口
Socket s = new Socket("192.168.0.100",10004);
//2.获取socket中的输出流
OutputStream os = s.getOutputStream();
//3.往输出流中传数据
os.write("服务端你好!".getBytes());
//4.接收服务端的反馈信息
InputStream is = s.getInputStream();
byte[] buf = new byte[1024];
int len = is.read(buf);
System.out.println(new String(buf,0,len));
//5.关闭socket服务
s.close();
}
}
class TcpServer {
public static void main(String[] args) throws Exception {
//1.建立ServerSocket服务,并监听一个端口
ServerSocket ss = new ServerSocket(10004);
//2.通过accept方法获取连接过来的客户端对象
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+"......已连接");
//3.通过使用客户端的输入流获取客户端发送过来的数据
InputStream is = s.getInputStream();
//创建一个字节数组用于存放客户端传递过来的数据
byte[] buf = new byte[1024];
//使用客户端对象的输入流,将流数据存放到字符数组中
int len = is.read(buf);
System.out.println(new String(buf,0,len));
//4.反馈信息给客户端
OutputStream os = s.getOutputStream();
Thread.sleep(5000);
os.write("服务端已收到你的信息,谢谢".getBytes());
//5.关闭客户端
s.close();
//ss.close();
}
}
示例2:
class TcpClient {
public static void main(String[] args) throws Exception, IOException {
//1.创建客户端的socket服务,指定主机和端口
Socket s = new Socket("192.168.0.100",10004);
//2.获取socket中的输出流
OutputStream os = s.getOutputStream();
//3.往输出流中传数据
os.write("服务端你好!".getBytes());
//4.接收服务端的反馈信息
InputStream is = s.getInputStream();
byte[] buf = new byte[1024];
int len = is.read(buf);
System.out.println(new String(buf,0,len));
//5.关闭socket服务
s.close();
}
}
class TcpServer {
public static void main(String[] args) throws Exception {
//1.建立ServerSocket服务,并监听一个端口
ServerSocket ss = new ServerSocket(10004);
//2.通过accept方法获取连接过来的客户端对象
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+"......已连接");
//3.通过使用客户端的输入流获取客户端发送过来的数据
InputStream is = s.getInputStream();
//创建一个字节数组用于存放客户端传递过来的数据
byte[] buf = new byte[1024];
//使用客户端对象的输入流,将流数据存放到字符数组中
int len = is.read(buf);
System.out.println(new String(buf,0,len));
//4.反馈信息给客户端
OutputStream os = s.getOutputStream();
Thread.sleep(5000);
os.write("服务端已收到你的信息,谢谢".getBytes());
//5.关闭客户端
s.close();
//ss.close();
}
}
示例3:
class TcpClient {
public static void main(String[] args) throws Exception, IOException {
//1.创建客户端的socket服务,指定主机和端口
Socket s = new Socket("192.168.0.100",10004);
//2.获取socket中的输出流
OutputStream os = s.getOutputStream();
//3.往输出流中传数据
os.write("服务端你好!".getBytes());
//4.接收服务端的反馈信息
InputStream is = s.getInputStream();
byte[] buf = new byte[1024];
int len = is.read(buf);
System.out.println(new String(buf,0,len));
//5.关闭socket服务
s.close();
}
}
class TcpServer {
public static void main(String[] args) throws Exception {
//1.建立ServerSocket服务,并监听一个端口
ServerSocket ss = new ServerSocket(10004);
//2.通过accept方法获取连接过来的客户端对象
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+"......已连接");
//3.通过使用客户端的输入流获取客户端发送过来的数据
InputStream is = s.getInputStream();
//创建一个字节数组用于存放客户端传递过来的数据
byte[] buf = new byte[1024];
//使用客户端对象的输入流,将流数据存放到字符数组中
int len = is.read(buf);
System.out.println(new String(buf,0,len));
//4.反馈信息给客户端
OutputStream os = s.getOutputStream();
Thread.sleep(5000);
os.write("服务端已收到你的信息,谢谢".getBytes());
//5.关闭客户端
s.close();
//ss.close();
}
}
示例4:
class ClientThread {
public static void main(String[] args) throws Exception, IOException {
if(args.length>1)
{
System.out.println("请选择一个jpg图像");
return;
}
File file = new File(args[0]);
if(!(file.exists()&&file.isFile()))
{
System.out.println("该文件不存在或者不是文件");
return;
}
if(!file.getName().endsWith(".jpg"))
{
System.out.println("图片格式错误,请重新选择");
return;
}
if(file.length()>1024*1024*5)
{
System.out.println("文件过大");
return;
}
Socket s = new Socket("192.168.0.100",10007);
FileInputStream fis = new FileInputStream("1.jpg");
OutputStream os = s.getOutputStream();
byte[] buf = new byte[1024];
int line = 0;
while((line = fis.read(buf))!=-1)
{
os.write(buf,0,line);
os.flush();
}
s.shutdownOutput();
InputStream is = s.getInputStream();
byte[] b = new byte[1024];
int num = is.read(b);
System.out.println(new String(b,0,num));
fis.close();
s.close();
}
}
class ServerThread {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(10007);
while(true)
{
Socket s = ss.accept();
new Thread(new UploadThread(s)).start();
}
}
}
class UploadThread implements Runnable
{
private Socket s;
public UploadThread(Socket s)
{
this.s = s;
}
@Override
public void run() {
try {
int count = 1;
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+"--已连接");
File file = new File(ip+"("+(count++)+").jpg");
while(file.exists())
{
file = new File(ip+"("+(count++)+").jpg");
}
FileOutputStream fos = new FileOutputStream(file);
InputStream is = s.getInputStream();
byte[] buf = new byte[1024];
int line = 0;
while((line = is.read(buf))!=-1)
{
fos.write(buf,0,line);
fos.flush();
}
OutputStream os = s.getOutputStream();
os.write((ip+"上传成功").getBytes());
fos.close();
s.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}