很久以前做的了,啟動(dòng)程序兩次,在單選框中選服務(wù)器點(diǎn)連接(一定要先點(diǎn)服務(wù)器-連接),在在另外一個(gè)界面中選客戶端點(diǎn)連接;
創(chuàng)新互聯(lián)主營噶爾網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶APP開發(fā),噶爾h5成都小程序開發(fā)搭建,噶爾網(wǎng)站營銷推廣歡迎噶爾等地區(qū)企業(yè)咨詢
import java.awt.Color;
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.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class QQ extends JFrame implements ActionListener{
public static void main(String args[]){
QQ qq=new QQ();
}
String input;
ServerSocket ss;
Socket s1,s2;
PrintWriter pw;
BufferedReader br;
private server s;
private client cc;
private JLabel l1,l2,l3,l4,l5;
private JRadioButton jb[]=new JRadioButton[2];
private JTextField jf1,jf2,jf3;
private JButton j1,j2,j3;
private JTextArea ja;
public QQ(){
super("聊天");
Container c=getContentPane();
c.setLayout(null);
l1=new JLabel("TCP通信程序");
l1.setFont(new Font("宋體",Font.BOLD,16));
l1.setBackground(Color.black);
l1.setSize(2000,20);
l1.setLocation(10,10);
c.add(l1);
String str1[]={"服務(wù)端","客戶端"};
ButtonGroup bg=new ButtonGroup();
for(int x=0;xstr1.length;x++)
{
jb[x]=new JRadioButton(str1[x]);
jb[x].setFont(new Font("宋體",Font.BOLD,15));
jb[x].setForeground(Color.black);
jb[x].setSize(80,40);
jb[x].setLocation(10+x*80,37);
bg.add(jb[x]);
c.add(jb[x]);
}
jb[0].setSelected(true);
l2=new JLabel("連接主機(jī)IP");
l2.setFont(new Font("宋體",Font.BOLD,16));
l2.setBackground(Color.black);
l2.setSize(120,20);
l2.setLocation(20, 80);
c.add(l2);
jf1=new JTextField("127.0.0.1");
jf1.setSize(220,30);
jf1.setLocation(120, 80);
c.add(jf1);
jf3=new JTextField("離線");
jf3.setSize(150,30);
jf3.setLocation(280, 40);
c.add(jf3);
l5=new JLabel("連接狀態(tài):");
l5.setFont(new Font("宋體",Font.BOLD,16));
l5.setBackground(Color.black);
l5.setSize(120,20);
l5.setLocation(200, 47);
c.add(l5);
j1=new JButton("連接");
j1.setSize(110,20);
j1.setLocation(360,85);
j1.addActionListener(this);
c.add(j1);
l3=new JLabel("接收到的信息");
l3.setFont(new Font("宋體",Font.BOLD,16));
l3.setBackground(Color.black);
l3.setSize(120,20);
l3.setLocation(20, 130);
c.add(l3);
ja=new JTextArea();
ja.setSize(250,200);
ja.setLocation(130, 130);
c.add(ja);
l4=new JLabel("發(fā)送信息");
l4.setFont(new Font("宋體",Font.BOLD,16));
l4.setBackground(Color.black);
l4.setSize(120,20);
l4.setLocation(20, 340);
c.add(l4);
jf2=new JTextField("gf");
jf2.setSize(220,30);
jf2.setLocation(120, 340);
c.add(jf2);
j2=new JButton("發(fā)送信息");
j2.setSize(110,20);
j2.setLocation(360,350);
j2.addActionListener(this);
c.add(j2);
j3=new JButton("結(jié)束連接");
j3.setSize(110,20);
j3.setLocation(360,110);
j3.addActionListener(this);
c.add(j3);
s=new server();
cc=new client();
j3.setEnabled(false);
j2.setEnabled(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,450);
setVisible(true);
setLocation(300,300);
}
public void actionPerformed(ActionEvent e) {
// TODO 自動(dòng)生成方法存根
if(e.getSource()==j1)
{
try{
if(jb[0].isSelected()==true)
{
input="";
s.start();
}
else {
input="";
cc.start();
}
}
catch(Exception ee)
{
jf3.setText("發(fā)生錯(cuò)誤");
}
}
if(e.getSource()==j2)
{
pw.write(jf2.getText()+"\n");
pw.flush();
}
if(e.getSource()==j3)
{
try
{
if(jb[0].isSelected()==true)
{ s1.close();
jf3.setText("離線");
j2.setEnabled(false);
j3.setEnabled(false);
}
else
{
s2.close();
jf3.setText("離線");
j2.setEnabled(false);
j3.setEnabled(false);
}
}
catch (Exception e1) {
// TODO 自動(dòng)生成 catch 塊
}
}
}
class server extends Thread{
public void run(){
try {
j1.setEnabled(false);
jf3.setText("正在連接中@");
ss=new ServerSocket(4000);
s1=ss.accept();
br=new BufferedReader(new InputStreamReader(s1.getInputStream()));
pw=new PrintWriter(s1.getOutputStream(),true);
// bs=new BufferedOutputStream(os);
while(true){
if(ss.isBound()==true){
jf3.setText("連接成功");
j2.setEnabled(true);
j3.setEnabled(true);
break;
}
}
while(true)
{
input=br.readLine();
if(input.length()0){
ja.append(input);
ja.append("\n");
}
}
} catch (Exception e) {
// TODO 自動(dòng)生成 catch 塊
}
}
}
class client extends Thread{
public void run(){
try {
j1.setEnabled(false);
jf3.setText("正在連接中@");
s2=new Socket(InetAddress.getByName(jf1.getText()),4000);
// s2=new Socket();
// s2.connect(new InetSocketAddress(jf1.getText(),21),1000);
br=new BufferedReader(new InputStreamReader(s2.getInputStream()));
pw=new PrintWriter(s2.getOutputStream(),true);
// bs=new BufferedOutputStream(os);
while(true){
if(s2.isConnected()==true){
jf3.setText("連接成功");
j2.setEnabled(true);
j3.setEnabled(true);
break;
}
}
input="";
while(true){
input=br.readLine();
if(input.length()0)
{
ja.append(input);
}
}
} catch (Exception e) {
// TODO 自動(dòng)生成 catch 塊
}
}
}
}
【ClientSocketDemo.java 客戶端Java源代碼】
import java.net.*;
import java.io.*;
public class ClientSocketDemo
{
//聲明客戶端Socket對(duì)象socket
Socket socket = null;
//聲明客戶器端數(shù)據(jù)輸入輸出流
DataInputStream in;
DataOutputStream out;
//聲明字符串?dāng)?shù)組對(duì)象response,用于存儲(chǔ)從服務(wù)器接收到的信息
String response[];
//執(zhí)行過程中,沒有參數(shù)時(shí)的構(gòu)造方法,本地服務(wù)器在本地,取默認(rèn)端口10745
public ClientSocketDemo()
{
try
{
//創(chuàng)建客戶端socket,服務(wù)器地址取本地,端口號(hào)為10745
socket = new Socket("localhost",10745);
//創(chuàng)建客戶端數(shù)據(jù)輸入輸出流,用于對(duì)服務(wù)器端發(fā)送或接收數(shù)據(jù)
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
//獲取客戶端地址及端口號(hào)
String ip = String.valueOf(socket.getLocalAddress());
String port = String.valueOf(socket.getLocalPort());
//向服務(wù)器發(fā)送數(shù)據(jù)
out.writeUTF("Hello Server.This connection is from client.");
out.writeUTF(ip);
out.writeUTF(port);
//從服務(wù)器接收數(shù)據(jù)
response = new String[3];
for (int i = 0; i response.length; i++)
{
response[i] = in.readUTF();
System.out.println(response[i]);
}
}
catch(UnknownHostException e){e.printStackTrace();}
catch(IOException e){e.printStackTrace();}
}
//執(zhí)行過程中,有一個(gè)參數(shù)時(shí)的構(gòu)造方法,參數(shù)指定服務(wù)器地址,取默認(rèn)端口10745
public ClientSocketDemo(String hostname)
{
try
{
//創(chuàng)建客戶端socket,hostname參數(shù)指定服務(wù)器地址,端口號(hào)為10745
socket = new Socket(hostname,10745);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
String ip = String.valueOf(socket.getLocalAddress());
String port = String.valueOf(socket.getLocalPort());
out.writeUTF("Hello Server.This connection is from client.");
out.writeUTF(ip);
out.writeUTF(port);
response = new String[3];
for (int i = 0; i response.length; i++)
{
response[i] = in.readUTF();
System.out.println(response[i]);
}
}
catch(UnknownHostException e){e.printStackTrace();}
catch(IOException e){e.printStackTrace();}
}
//執(zhí)行過程中,有兩個(gè)個(gè)參數(shù)時(shí)的構(gòu)造方法,第一個(gè)參數(shù)hostname指定服務(wù)器地址
//第一個(gè)參數(shù)serverPort指定服務(wù)器端口號(hào)
public ClientSocketDemo(String hostname,String serverPort)
{
try
{
socket = new Socket(hostname,Integer.parseInt(serverPort));
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
String ip = String.valueOf(socket.getLocalAddress());
String port = String.valueOf(socket.getLocalPort());
out.writeUTF("Hello Server.This connection is from client.");
out.writeUTF(ip);
out.writeUTF(port);
response = new String[3];
for (int i = 0; i response.length; i++)
{
response[i] = in.readUTF();
System.out.println(response[i]);
}
}
catch(UnknownHostException e){e.printStackTrace();}
catch(IOException e){e.printStackTrace();}
}
public static void main(String[] args)
{
String comd[] = args;
if(comd.length == 0)
{
System.out.println("Use localhost(127.0.0.1) and default port");
ClientSocketDemo demo = new ClientSocketDemo();
}
else if(comd.length == 1)
{
System.out.println("Use default port");
ClientSocketDemo demo = new ClientSocketDemo(args[0]);
}
else if(comd.length == 2)
{
System.out.println("Hostname and port are named by user");
ClientSocketDemo demo = new ClientSocketDemo(args[0],args[1]);
}
else System.out.println("ERROR");
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
【ServerSocketDemo.java 服務(wù)器端Java源代碼】
import java.net.*;
import java.io.*;
public class ServerSocketDemo
{
//聲明ServerSocket類對(duì)象
ServerSocket serverSocket;
//聲明并初始化服務(wù)器端監(jiān)聽端口號(hào)常量
public static final int PORT = 10745;
//聲明服務(wù)器端數(shù)據(jù)輸入輸出流
DataInputStream in;
DataOutputStream out;
//聲明InetAddress類對(duì)象ip,用于獲取服務(wù)器地址及端口號(hào)等信息
InetAddress ip = null;
//聲明字符串?dāng)?shù)組對(duì)象request,用于存儲(chǔ)從客戶端發(fā)送來的信息
String request[];
public ServerSocketDemo()
{
request = new String[3]; //初始化字符串?dāng)?shù)組
try
{
//獲取本地服務(wù)器地址信息
ip = InetAddress.getLocalHost();
//以PORT為服務(wù)端口號(hào),創(chuàng)建serverSocket對(duì)象以監(jiān)聽該端口上的連接
serverSocket = new ServerSocket(PORT);
//創(chuàng)建Socket類的對(duì)象socket,用于保存連接到服務(wù)器的客戶端socket對(duì)象
Socket socket = serverSocket.accept();
System.out.println("This is server:"+String.valueOf(ip)+PORT);
//創(chuàng)建服務(wù)器端數(shù)據(jù)輸入輸出流,用于對(duì)客戶端接收或發(fā)送數(shù)據(jù)
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
//接收客戶端發(fā)送來的數(shù)據(jù)信息,并顯示
request[0] = in.readUTF();
request[1] = in.readUTF();
request[2] = in.readUTF();
System.out.println("Received messages form client is:");
System.out.println(request[0]);
System.out.println(request[1]);
System.out.println(request[2]);
//向客戶端發(fā)送數(shù)據(jù)
out.writeUTF("Hello client!");
out.writeUTF("Your ip is:"+request[1]);
out.writeUTF("Your port is:"+request[2]);
}
catch(IOException e){e.printStackTrace();}
}
public static void main(String[] args)
{
ServerSocketDemo demo = new ServerSocketDemo();
}
}
首先 java網(wǎng)站是基于jdk運(yùn)行的。你得先裝jdk。去官網(wǎng)下載一個(gè)jdk安裝。然后你得編譯你的項(xiàng)目,打包到容器(tomcat,weblogic,jboss等)中運(yùn)行即可。再提醒一下就是,看看你的源代碼有木有用到數(shù)據(jù)庫或者其他類型的服務(wù)。如果有的話,你得裝數(shù)據(jù)庫或服務(wù),并且初始化數(shù)據(jù)。再啟動(dòng)你的你的容器。
是的。因?yàn)樵创a,gitlab上下載的代碼,后綴名是java的就是源代碼,可以用IDEA打開查看,查看某個(gè)方法可以實(shí)現(xiàn)跳轉(zhuǎn)。所以java服務(wù)器代碼倒出來,就是源碼。