-----------ASP.Net+Unity开发----.Net培训------------------
TCP传输:两个端点的建立连接后会有一个传输数据的通道,这通道称为流,而且是建立在网络基础上的流,称之为socket流。该流中既有读取,也有写入。
tcp的两个端点:一个是客户端,一个是服务端。
客户端:对应的对象,Socket
服务端:对应的对象,ServerSocket
TCP客户端:
1,建立tcp的socket服务,最好明确具体的地址和端口。这个对象在创建时,就已经可以对指定ip和端口进行连接(三次握手)。
2,如果连接成功,就意味着通道建立了,socket流就已经产生了。只要获取到socket流中的读取流和写入流即可,只要通过getInputStream和getOutputStream就可以获取两个流对象。
3,关闭资源。
import java.net.*;
import java.io.*;
//需求:客户端给服务器端发送一个数据。
class TcpClient{
public static void main(String[] args) throws Exception{
Socket s = new Socket("10.1.31.69",10002);
OutputStream out = s.getOutputStream();//获取了socket流中的输出流对象。
out.write("tcp演示,哥们又来了!".getBytes());
s.close();
}
}
TCP服务端:
1,创建服务端socket服务,并监听一个端口。
2,服务端为了给客户端提供服务,获取客户端的内容,可以通过accept方法获取连接过来的客户端对象。
3,可以通过获取到的socket对象中的socket流和具体的客户端进行通讯。
4,如果通讯结束,关闭资源。注意:要先关客户端,再关服务端。
class TcpServer{
public static void main(String[] args) throws Exception{
ServerSocket ss = new ServerSocket(10002);//建立服务端的socket服务
Socket s = ss.accept();//获取客户端对象
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+".....connected");
// 可以通过获取到的socket对象中的socket流和具体的客户端进行通讯。
InputStream in = s.getInputStream();//读取客户端的数据,使用客户端对象的socket读取流
byte[] buf = new byte[1024];
int len = in.read(buf);
String text = new String(buf,0,len);
System.out.println(text);
// 如果通讯结束,关闭资源。注意:要先关客户端,在关服务端。
s.close();
ss.close();
}
}
TCP小程序示例:
public static void sop(Object obj)
{
System.out.println(obj);
}
一、
//TCP传输数据练习 客户端
public void tcpclient()
{
try
{
Socket socket = new Socket("192.168.1.254",10003);
OutputStream out = socket.getOutputStream();
out.write("TCP is coming".getBytes());
socket.close();
}
catch(Exception e)
{
sop(e.toString());
}
}
//TCP传输数据练习 服务端 可以循环接受数据,但是不可以并发接收数据
public void tcpSever()
{
try
{
ServerSocket ss= new ServerSocket(10003);
while(true)
{
Socket s= ss.accept();
String ip= s.getInetAddress().getHostAddress();
sop(ip+"..connected");
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
sop(new String(buf,0,len));
s.close();
}
}
catch(Exception e)
{
sop(e.toString());
}
}
二、
//TCP传输 客户端发送消息,服务端接受到消息后,给客户端返回一个消息 服务端
public void tcpServer()
{
try
{
ServerSocket ss = new ServerSocket(10004);
Socket s = ss.accept();
byte[] buf = new byte[1024];
InputStream in = s.getInputStream();
int len = in.read(buf);
sop(new String(buf,0,len));
OutputStream out = s.getOutputStream();
out.write("服务器收到,你也好".getBytes());
s.close();
ss.close();//可选
}
catch(Exception e)
{
sop(e.toString());
}
}
//TCP传输 客户端发送消息,服务端接受到消息后,给客户端返回一个消息 客户端
public void tcpClient1()
{
try
{
Socket s = new Socket("192.168.0.234",10004);
OutputStream out = s.getOutputStream();
out.write("服务端你好".getBytes());
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
sop(new String (buf,0,len));
s.close();
}
catch(Exception e)
{
sop(e.toString());
}
}
三、
//TCP传输,文本转换器 ,客户端向服务端发送字符,转成大写发送回来。 服务端
public static void tcpsever2()
{
try
{
ServerSocket ss = new ServerSocket(10005);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
sop(ip+"...connnected");
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter pw = new PrintWriter(s.getOutputStream());
String line = null;
while((line=bufIn.readLine())!=null)
{
sop(line);
pw.println(line.toUpperCase());
}
s.close();
ss.close();
}
catch(Exception e)
{
sop(e.toString());
}
}
//TCP传输,文本转换器 ,客户端向服务端发送字符,转成大写发送回来。 客户端
public static void tcpclient2()
{
try
{
Socket s = new Socket("192.168.0.1",10005);
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter bufOut = new PrintWriter(s.getOutputStream(),true);
String line = null;
while((line=bufr.readLine())!=null)
{
if("over".equals(line))
break;
//byte[] buf = line.getBytes();
bufOut.println(line);
String str=bufIn.readLine();
sop("server:"+str);
}
bufr.close();
s.close();
}
catch(Exception e)
{
sop(e.toString());
}
}
四、
//TCP 客户端上传文件到服务端 服务端
public static void textServer()
{
try
{
ServerSocket ss = new ServerSocket(10006);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
sop(ip+"...connected");
BufferedReader bufIn = new BufferedReader (new InputStreamReader(s.getInputStream()));
PrintWriter out = new PrintWriter(new FileWriter("e:\\dengdeng.txt",true));
String line = null;
while((line=bufIn.readLine())!=null)
{
out.println(line);
}
PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
pw.println("Succeed");
out.close();
s.close();
ss.close();
}
catch(Exception e)
{
sop(e.toString());
}
}
//TCP 客户端上传 文件到服务端 客户端
public static void textClient()
{
try
{
Socket s = new Socket("192.168.0.125",10006);
BufferedReader bufr = new BufferedReader(new FileReader("e:\\demo.txt"));
PrintWriter out = new PrintWriter(s.getOutputStream());
String line = null;
while((line= bufr.readLine())!=null)
{
out.println(line);
}
s.shutdownOutput();
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = bufIn.readLine();
sop(str);
bufr.close();
s.close();
}
catch(Exception e)
{
sop(e.toString());
}
}
五、
//TCP 上传图片 只能上传一次 无法实现并发上传,以及多次上传 服务端
public static void picserver()
{
try
{
ServerSocket ss = new ServerSocket(10007);
Socket s =ss.accept();
InputStream in = s.getInputStream();
FileOutputStream fos = new FileOutputStream("e:\\server.bmp");
byte[] buf = new byte[1024];
int len=0;
while((len=in.read(buf))!=-1)
{
fos.write(buf,0,len);
}
OutputStream out = s.getOutputStream();
out.write("上传成功".getBytes());
fos.close();
s.close();
ss.close();
}
catch(Exception e)
{
sop(e.toString());
}
}
//TCP 上传图片 只能上传一次 无法实现并发上传,以及多次上传 客户端
public static void picclient()
{
try
{
Socket s = new Socket("192.168.0.125",10007);
FileInputStream fis = new FileInputStream("e:\\1.bmp");
OutputStream out = s.getOutputStream();
byte[] buf = new byte[1024];
int len =0;
while((len=fis.read(buf))!=-1)
{
out.write(buf,0,len);
}
s.shutdownOutput();
InputStream in = s.getInputStream();
byte[] bufIn = new byte[1024];
int num = in.read(bufIn);
sop(new String(bufIn,0,num));
fis.close();
s.close();
}
catch(Exception e)
{
sop(e.toString());
}
}
六、
//TCP 上传图片 --客户端实现选择图片--服务端实现可以并发接收图片 -- 利用多线程知识 --客户端函数:
//服务端为类
public static void picClient(String[] args)
{
if(args.length !=1)
{
sop("请选择一个jpg格式图片");
return;
}
File file = new File(args[0]);
if(!(file.exists() && file.isFile()))
{
sop("该文件不存或者不是文件");
return;
}
if(!file.getName().endsWith(".jpg"))
{
sop("图片格式错误");
return;
}
if(file.length()>1024*1024*5)
{
sop("文件过大");
return;
}
try
{
Socket s = new Socket("192.168.1.125",10008);
FileInputStream fis = new FileInputStream(file);
OutputStream out = s.getOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while((len=fis.read(buf))!=-1)
{
out.write(buf,0,len);
}
s.shutdownOutput();
InputStream in = s.getInputStream();
byte[] bufIn = new byte[1024];
int num = in.read(bufIn);
sop(new String(bufIn,0,num));
fis.close();
s.close();
}
catch(Exception e)
{
sop(e.toString());
}
}
//TCP 上传图片 --客户端实现选择图片--服务端实现可以并发接收图片 -- 利用多线程知识 --客户端函数:
//服务端为类 服务端:
class PicThread implements Runnable{
private Socket s;
public PicThread(Socket s)
{
this.s = s;
}
public void run()
{
int count = 1;
String ip = s.getInetAddress().getHostAddress();
try
{
System.out.println(ip+"...connected");
InputStream in = s.getInputStream();
File dir = new File("e:\\pic");
File file = new File(dir,ip+"("+count+")"+".jpg");
while(file.exists())
file = new File(dir,ip+"("+(count++)+")"+".jpg");
FileOutputStream fos = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len = 0;
while((len=in.read(buf))!=-1)
{
fos.write(buf, 0, len);
}
OutputStream out = s.getOutputStream();
out.write("上传成功".getBytes());
fos.close();
s.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
-----------ASP.Net+Unity开发----.Net培训------------------