//TCP简单通信:
TCP发送数据:
对于客户端:
1.需要有本地的文件;
2.需要本地的流,往外输出的流;
3.需要知道服务器的ip和服务器的端口号,并封装成Socket;
对于服务器:
4. ServerSocket接收的服务端;
5. Socket接收的服务端socketl;
6. InputStream输入流;
最初的写法:
Close后需要关闭流就不能throws异常,需要用个try-catch来抛出异常:修改后客户端和服务端的写法:
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import org.junit.Test;
import org.omg.CORBA.portable.InputStream;
public class TCPTest {
//客户端
@Test
public void client() {
Socket socket = null;
OutputStream os = null;
try {
InetAddress inet = InetAddress.getByName("127.0.0.1");
socket = new Socket(inet, 8888);
os = socket.getOutputStream();
os.write("你好,我是客户端!".getBytes());
} catch (Exception e) {
// TODO: handle exception
}finally {
if(os!=null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket!=null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//服务端
@Test
public void server(){
ServerSocket ss = null;
Socket socket= null;
InputStream is= null;
//下面这样写有中文就会出现乱码
// byte[] buffer = new byte[200];
// int len;
// while((len = is.read(buffer))!=-1) {
// String str = new String(buffer,0,len);
// System.out.print(str);
// }
//为防止有的输入输出流被劈成两半用下面这样的方式
ByteArrayOutputStream baos = null;
try {
ss = new ServerSocket(8888);
socket = ss.accept();
is = (InputStream) socket.getInputStream();
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[5];
int len;
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
System.out.println(baos.toString());
} catch (Exception e) {
// TODO: handle exception
}finally {
//关闭资源
if(baos!=null) {
try {
baos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(is!=null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(socket!=null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ss!=null) {
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
*创建UDP通信:
*写客户端步骤:
*1.构建InetSocketAddress建立ip地址和端口
*2.建立DatagramSocket构建socket套接字
*3.写数据建立数据包,将数据放入数据包
*4.发送
*/
*服务器:
*1.DatagramSocket创建套接字接上端口
*2.构建包接收的数组,创建包
*3.接收包
*4.转化包中的数据,必要时转化ip
*/
public class Client {
public static void main(String[] args) {
//1.建立TCP
java.net.InetSocketAddress inetSocketAddress = new InetSocketAddress("192.168.56.1",6666);
//2.建立套接字
try {
java.net.DatagramSocket socket = new DatagramSocket();
while(true) {
//3.建立数据包
String msg = new String("你好!服务器!");
byte[] buffer = msg.getBytes();
DatagramPacket packet = new DatagramPacket(buffer, buffer.length,inetSocketAddress);
//4.发送出去
socket.send(packet);
System.out.println("发送成功!");
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class Sever {
public static void main(String[] args) {
//创建socket接口
try {
//1.创建套接字
java.net.DatagramSocket dSocket = new DatagramSocket(6666);
while(true) {
byte[] data = new byte[10];
//2.创建包
DatagramPacket dPacket = new DatagramPacket(data, data.length);
dSocket.receive(dPacket);
String msg = new String(dPacket.getData());
String ms = dPacket.getAddress().toString();
System.out.println("dPacket.getData()"+msg);
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}