//客户端
package com.netPro.tcpCommunicate;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class TClient {
public static void main(String[] args) throws Exception {
//客户端
//要知道服务器的地址和端口号
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 8888;
//创建socket连接
Socket socket = new Socket(serverIP, port);
//发送消息 IO流
OutputStream os = socket.getOutputStream();
os.write("你好,客户端向你发送了一条消息!".getBytes());
System.out.println("发送完毕!");
os.close();
socket.close();
}
}
//服务器
package com.netPro.tcpCommunicate;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TServer {
public static void main(String[] args) throws IOException {
//服务端
//有个地址
ServerSocket serverSocket = new ServerSocket(8888);
//等待客户端连接
Socket socket = serverSocket.accept();
//读取消息
InputStream is = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] date = new byte[1024];
int count = 0;
while ((count = is.read(date)) != -1){
baos.write(date,0,count);
}
System.out.println(baos.toString());
baos.close();
is.close();
socket.close();
serverSocket.close();
}
}
//客户端
package com.netPro.tcpFileDown;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class TClient {
public static void main(String[] args) {
try {
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 8088;
//创建socket服务
Socket socket = new Socket(serverIP,port);
//创建一个输出流
OutputStream os = socket.getOutputStream();
//读取文件
FileInputStream fis = new FileInputStream(new File("1.jpg"));
byte[] date = new byte[1024];
int count;
while ((count=fis.read(date)) != -1){
os.write(date,0,count);
}
//通知服务器我已经结束了
socket.shutdownOutput();
//确定服务器接收完毕 断开连接
InputStream is = socket.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] dates = new byte[1024];
int counts;
while ((counts = is.read(dates)) != -1){
bos.write(dates,0,counts);
}
System.out.println(bos.toString());
bos.close();
is.close();
fis.close();
os.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//服务器
package com.netPro.tcpFileDown;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TServer {
public static void main(String[] args) {
try {
//创建服务
ServerSocket serverSocket = new ServerSocket(8088);
//阻塞式监听 一直等待
Socket socket = serverSocket.accept();
//获取输入流
InputStream is = socket.getInputStream();
//文件输出
FileOutputStream fos = new FileOutputStream(new File("receive.jpg"));
byte[] date = new byte[1024];
int count;
while ((count=is.read(date)) != -1){
fos.write(date,0,count);
}
//通知客户端我已经接收完毕 可以断开了
OutputStream os = socket.getOutputStream();
os.write("我已经接收完毕,可以断开连接了".getBytes());
os.close();
fos.close();
is.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//发送方
package com.netPro.UDPSendMesage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
public class ClientSend {
public static void main(String[] args) {
try {
InetSocketAddress localhost = new InetSocketAddress("localhost",6666);
DatagramSocket socket = new DatagramSocket(8888);
while (true){
//控制台输入数据
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String date = br.readLine();
byte[] dates = date.getBytes();
DatagramPacket packet = new DatagramPacket(dates,0,dates.length,localhost);
socket.send(packet);
if (date.equals("bye")){
break;
}
}
socket.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//接收方
package com.netPro.UDPSendMesage;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class ClientReceive {
public static void main(String[] args) {
try {
DatagramSocket socket = new DatagramSocket(6666);
while (true){
//准备接收数据
byte[] date = new byte[1024];
DatagramPacket packet = new DatagramPacket(date,0,date.length);
socket.receive(packet);
byte[] receive = packet.getData();
System.out.println(new String(receive,0, packet.getLength()));
if (receive.equals("bye")){
break;
}
}
socket.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//发送方
package com.netPro.UDPChat;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
public class EndSend extends JFrame implements Runnable{
DatagramSocket socket = null;
BufferedReader reader = null;
InetSocketAddress toAddress = null;
InputStreamReader isr = null;
DatagramPacket packet = null;
int fromPort;
String toIP;
int toPort;
public EndSend(int fromPort, String toIP, int toPort){
this.fromPort = fromPort;
this.toIP = toIP;
this.toPort = toPort;
try {
socket = new DatagramSocket(fromPort);
isr = new InputStreamReader(System.in);
reader = new BufferedReader(isr);
toAddress = new InetSocketAddress(toIP,toPort);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
try {
String date = reader.readLine();
byte[] dates = date.getBytes();
packet = new DatagramPacket(dates,0,dates.length,toAddress);
socket.send(packet);
if (date.equals("bye")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
//接收
package com.netPro.UDPChat;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class EndReceive implements Runnable{
DatagramSocket socket = null;
DatagramPacket packet = null;
int Port;
String msgFrom;
public EndReceive(){
}
public EndReceive(int Port,String msgFrom){
this.Port = Port;
this.msgFrom = msgFrom;
try {
socket = new DatagramSocket(this.Port);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
try {
byte[] date = new byte[1024];
packet = new DatagramPacket(date,0,date.length);
socket.receive(packet);
byte[] receive = packet.getData();
System.out.println(msgFrom+":"+new String(receive,0, packet.getLength()));
if (receive.equals("bye")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
测试类
package com.netPro.UDPChat;
public class TestStudent {
public static void main(String[] args) {
new Thread(new EndSend(777,"127.0.0.1",888)).start();
new Thread(new EndReceive(666,"teacher")).start();
}
}
package com.netPro.UDPChat;
public class TestTeacher {
public static void main(String[] args) {
new Thread(new EndSend(555,"127.0.0.1",666)).start();
new Thread(new EndReceive(888,"student")).start();
}
}