【ClientSocketDemo.java 客戶端Java源代碼】
創(chuàng)新互聯(lián)主要從事網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)肇源,十年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):13518219792
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();
}
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class GameTest extends JFrame implements ActionListener{
/*
* 新建一個(gè)主面板(這個(gè)類可能是自定義的,本程序和API中沒有)。
*/
MainPanel j=new MainPanel();
JButton jPreview;
JLabel label;
Container container;
JPanel panel;
/**
* 主函數(shù)
* @param args
*/
public static void main(String[] args) {
//運(yùn)行程序
new GameTest();
}
/**
* 構(gòu)造函數(shù)。
*
*/
public GameTest()
{
//新建一個(gè)標(biāo)題為“拼圖”的窗口
JFrame fr =new JFrame("拼圖");
//獲取窗口容器。
container=fr.getContentPane();
//創(chuàng)建菜單條
JMenuBar jMenuBar=new JMenuBar();
//以下初始化菜單,并且設(shè)置快捷鍵和添加監(jiān)聽器。
JMenu jMenuGame=new JMenu("游戲(G)");
jMenuGame.setMnemonic('g');
JMenuItem jMenuItemStart = new JMenuItem("開始(S)");
jMenuItemStart.setMnemonic('s');
jMenuItemStart.addActionListener(this);
JMenuItem jMenuItemExit=new JMenuItem("退出(E)");
jMenuItemExit.setMnemonic('e');
jMenuItemExit.addActionListener(this);
jMenuGame.add(jMenuItemStart);
jMenuGame.add(jMenuItemExit);
//初始化按鈕并設(shè)置快捷鍵和添加監(jiān)聽器
JButton jChoice=new JButton("選圖(X)");
jChoice.setMnemonic('x');
jChoice.addActionListener(this);
jPreview=new JButton("預(yù)覽(P)");
jPreview.setMnemonic('p');
jPreview.addActionListener(this);
//將菜單和按鈕添加到菜單條中
jMenuBar.add(jMenuGame);
jMenuBar.add(jChoice);
jMenuBar.add(jPreview);
//將菜單條設(shè)為該窗口的主菜單
fr.setJMenuBar(jMenuBar);
//將主面板添加到該窗口的容器中。
container.add(j);
//設(shè)置大小
fr.setSize(315,360 );
fr.setVisible(true);
//設(shè)置默認(rèn)關(guān)閉方式。
fr.setDefaultCloseOperation(3);
}
/**
* 事件處理函數(shù)。
*/
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="開始(S)")
{
j.Start();
}
if(e.getActionCommand()=="預(yù)覽(P)")
{
j.setVisible(false);
panel=new JPanel();
Icon icon=new ImageIcon("pictrue/pic"+"_"+MainPanel.pictureID+".jpg");
label=new JLabel(icon);
label.setBounds(300, 300, 0, 0);
panel.add(label);
panel.setSize(300, 300);
panel.setVisible(true);
this.container.add(panel);
jPreview.setText("返回(P)");
}
if(e.getActionCommand()=="返回(P)")
{
panel.setVisible(false);
j.setVisible(true);
j.repaint();
jPreview.setText("預(yù)覽(P)");
}
if(e.getActionCommand()=="退出(E)")
{
System.exit(0);
}
if(e.getActionCommand()=="選圖(X)")
{
//初始化選擇框,并提供選擇。
Choice pic = new Choice();
pic.add("七里香");
pic.add("依然范特西");
pic.add("八度空間");
pic.add("十一月的肖邦");
pic.add("魔杰座");
pic.add("葉惠美");
pic.add("我很忙");
int i=JOptionPane.showConfirmDialog(this, pic, "選擇圖片", JOptionPane.OK_CANCEL_OPTION);
if(i==JOptionPane.YES_OPTION)
{
//選擇圖片
MainPanel.pictureID=pic.getSelectedIndex()+1;
j.removeAll();
j.reLoadPicture();
j.repaint();
}
}
}
}
import java.awt.*; \x0d\x0aimport java.awt.event.*; \x0d\x0aimport javax.swing.*; \x0d\x0apublic class Els extends JFrame implements KeyListener,Runnable \x0d\x0a{\x0d\x0apublic Els() \x0d\x0a{ \x0d\x0asetSize(240,320); \x0d\x0asetUndecorated(true); \x0d\x0aint H=(int) (this.getToolkit().getScreenSize().height); \x0d\x0aint W=(int) (this.getToolkit().getScreenSize().width); \x0d\x0asetLocation(W/2-120,H/2-160); \x0d\x0asetResizable(false); \x0d\x0asetDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); \x0d\x0a} \x0d\x0apublic void paint(Graphics g) \x0d\x0a{ \x0d\x0asuper.paint(g); \x0d\x0aGraphics2D g2=(Graphics2D)g; \x0d\x0a g2.setColor(Color.darkGray.brighter().brighter()); \x0d\x0a \x0d\x0a //繪制小框格 \x0d\x0a for(int m=192;m
回答于?2022-11-16