1.服务端向客户端传递一个文件,客户端读取文件,并保存到本地。
程序具体实现:将根目录下的socket.txt文件,拷贝至另一个项目的根目录下
实现思路:在AdvanceProgramming项目下创建FileServert类作为服务器端,然后再zhongruan项目下创建FileClient类作为客户端接收文件,以下是代码
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class FileServer {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(6666);
Socket so = ss.accept();
//获得输出流
OutputStream os = so.getOutputStream();
//读取文件信息
FileInputStream fis = new FileInputStream("socket.txt");
byte[] bs = new byte[10];
int len = 0;
while((len = fis.read(bs))!=-1){
System.out.println(new String(bs,0,len));
//发送信息给客户端
os.write(bs, 0, len);
os.flush();
}
os.close();
}
}
public class FileClient {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Socket s = new Socket("127.0.0.1",6666);
InputStream fs = s.getInputStream();
FileOutputStream fos = new FileOutputStream("socket.txt");
byte[] bs = new byte[10];
int len=0;
while((len=fs.read(bs))!=-1){
System.out.println(new String(bs,0,len));
fos.write(bs, 0, len);
fos.flush();
}
fos.close();
}
}
2.实现简易聊天室
实现点对点聊天
以下是代码
主要思想是将1发送给2
和2发送给1变为两个端口的传输
端口6666负责2的信息发送给1
端口6667负责将1的信息发送给2
发送的时候打开端口连接!
package homeworkSocket;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.awt.event.ActionEvent;
public class ChatRoomClient1 extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextArea textArea;
/**
* Launch the application.
*/
public void accept() throws Exception{
String str = null;
ServerSocket ss = new ServerSocket(6666);
Socket s = ss.accept();
InputStream is = s.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
str = br.readLine();
textArea.append("客户端:"+str+"\n");
ss.close();
}
public void send(String str) throws Exception{
Socket s = new Socket("127.0.0.1",6667);
OutputStream os = s.getOutputStream();
BufferedWriter bs = new BufferedWriter(new OutputStreamWriter(os));
System.out.println(str);
bs.write(str);
bs.close();
os.close();
s.close();
//accept();
//return str;
}
public static void main(String[] args) {
try {
ChatRoomClient1 frame = new ChatRoomClient1();
frame.setVisible(true);
// Scanner in = new Scanner(System.in);
// in.next();
while(true)
frame.accept();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the frame.
*/
public ChatRoomClient1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
textArea = new JTextArea();
textArea.setRows(15);
textArea.setColumns(25);
contentPane.add(new JScrollPane(textArea), BorderLayout.WEST);
textField = new JTextField();
contentPane.add(textField, BorderLayout.SOUTH);
textField.setColumns(10);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.EAST);
JButton btnNewButton = new JButton("发送");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.append("服务器:"+textField.getText()+"\n");
try {
send(textField.getText());
textField.setText("");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
panel.add(btnNewButton);
JButton btnNewButton_1 = new JButton("退出");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
panel.add(btnNewButton_1);
}
}
package homeworkSocket;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.awt.event.ActionEvent;
public class ChatRoomClient2 extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextArea textArea;
/**
* Launch the application.
*/
public void accept() throws Exception{
String str = null;
//for(int i=0;i<1000000;i++);
ServerSocket ss = new ServerSocket(6667);
Socket s = ss.accept();
InputStream is = s.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
str = br.readLine();
textArea.append("服务器:"+str+"\n");
ss.close();
}
public void send(String str) throws Exception{
Socket s = new Socket("127.0.0.1",6666);
OutputStream os = s.getOutputStream();
BufferedWriter bs = new BufferedWriter(new OutputStreamWriter(os));
System.out.println(str);
bs.write(str);
bs.close();
os.close();
s.close();
//return str;
}
public static void main(String[] args) {
try {
ChatRoomClient2 frame = new ChatRoomClient2();
frame.setVisible(true);
while(true)
frame.accept();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the frame.
*/
public ChatRoomClient2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
textArea = new JTextArea();
textArea.setRows(15);
textArea.setColumns(25);
contentPane.add(new JScrollPane(textArea), BorderLayout.WEST);
textField = new JTextField();
contentPane.add(textField, BorderLayout.SOUTH);
textField.setColumns(10);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.EAST);
JButton btnNewButton = new JButton("发送");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.append("客户端"+textField.getText()+"\n");
try {
send(textField.getText());
textField.setText("");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
panel.add(btnNewButton);
JButton btnNewButton_1 = new JButton("退出");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
panel.add(btnNewButton_1);
}
}