Socket (套接字建立类)
InetAddress (IP地址)
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class SingleCilent extends JFrame implements ActionListener{
public JLabel[] jLabel;
public JTextArea[] jTextArea;
public JButton[] jbutton;
public JTextField[] jT;
public JScrollPane JS;
public Socket socket;
public BufferedReader bin;
public PrintStream pout;
public SingleCilent(){
super("简单的连接服务器实例");
jLabel = new JLabel[2];
jLabel[0] = new JLabel("服务器IP");
jLabel[1] = new JLabel("端口");
jLabel[0].setFont(new Font("宋体",Font.PLAIN,13));
jLabel[1].setFont(new Font("宋体",Font.PLAIN,13));
jLabel[0].setBounds(15,5,60,30);
jLabel[1].setBounds(200,5,40,30);
jbutton = new JButton[2];
jbutton[0] = new JButton("连接服务器");
jbutton[1] = new JButton("发送信息");
jbutton[0].addActionListener(this);
jbutton[1].addActionListener(this);
jbutton[0].setFont(new Font("宋体",Font.PLAIN,13));
jbutton[1].setFont(new Font("宋体",Font.PLAIN,13));
jbutton[0].setBounds(325,5,100,30);
jbutton[1].setBounds(325,45,100,30);
jT = new JTextField[3];
jT[0] = new JTextField("**.**.**.**"); //这里换成自己的需要连接的IP地址;
jT[0].setFont(new Font("宋体",Font.PLAIN,13));
jT[1] = new JTextField("80"); //这里换成自己所需要连接的端口;
jT[1].setFont(new Font("宋体",Font.PLAIN,13));
jT[0].setBounds(85,5,100,30);
jT[1].setBounds(245,5,60,30);
jT[2] = new JTextField("");
jT[2].setFont(new Font("宋体",Font.PLAIN,13));
jT[2].setBounds(15,45,290,30);
jTextArea = new JTextArea[1];
jTextArea[0] = new JTextArea("");
jTextArea[0].setFont(new Font("宋体",Font.PLAIN,13));
JS = new JScrollPane(jTextArea[0]);
JS.setBounds(15,85,410,120);
Container container = this.getContentPane();
container.setLayout(null);
container.add(jbutton[0]);
container.add(jbutton[1]);
container.add(JS);
container.add(jLabel[0]);
container.add(jLabel[1]);
container.add(jT[0]);
container.add(jT[1]);
container.add(jT[2]);
this.setSize(450,250);
this.setVisible(true);
}
public static void main(String[] args) {
SingleCilent aa = new SingleCilent();
}
public void actionPerformed(ActionEvent actionEvent){ //进行一些简单的事件捕捉
if(actionEvent.getSource()==jbutton[0]){
try{
InetAddress ip = InetAddress.getByName(jT[0].getText()); //实例化IP地址类的对象, 然后初始化IP地址
int port = Integer.parseInt(jT[1].getText()); //port是获取到需要访问的服务器的端口 进行了一个自动装箱的转换
socket = new Socket(ip,port); //建立Socket对象 建立套接字;
jTextArea[0].append("开始连接!");
}catch(IOException e){
jTextArea[0].append("服务器端口连接失败");
}
if(socket!=null){ //当socket套接字建立成功时执行以下代码;
try{
bin = new BufferedReader(new InputStreamReader(socket.getInputStream())); //建立一个面向服务器的缓冲输入流 socket.getInputStream() 是从套接字中返回一个输入流
pout = new PrintStream(socket.getOutputStream()); //建立一个面向服务器的缓冲输出流 socket.getOutputStream() 是从套接字中返回一个输出流
String str = "来自"+InetAddress.getLocalHost().toString()+"消息"; // InetAddress.getLocalHost()是返回本机的IP地址,如果IP没有地址的话返回默认地址
pout.println(str); //输出“来自”+本机IP+“消息”;
bin.close(); //关闭流;
pout.close();
jTextArea[0].append("向服务器发送"+str+"\n");
socket.close();
}catch(IOException e){
jTextArea[0].append("发送异常!");
}
}
}
else if(actionEvent.getSource()==jbutton[1]){
;
}
}
}