日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

javasocket编程(javasocket通信)

發(fā)布時(shí)間:2023/12/10 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 javasocket编程(javasocket通信) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Java網(wǎng)絡(luò)編程-JavaSocket編程是什么呢?

??Socket傳輸模式

Sockets有兩種主要的操作方式:面向連接的和無(wú)連接的。面向連接的sockets操作就像一部電話,他們必須建立一個(gè)連接和一人呼叫。所有的事情在到達(dá)時(shí)的順序與它們出發(fā)時(shí)的順序時(shí)一樣。

無(wú)連接的sockets操作就像是一個(gè)郵件投遞,沒(méi)有什么保證,多個(gè)郵件可能在到達(dá)時(shí)的順序與出發(fā)時(shí)的順序不一樣。

Javasocket入門(mén)編程是什么?

??java原文件: import java。net。*; import java。io。*; public class ServerApp { public static void main(String args[]) { try { boolean flag=true; Socket clientSocket=null; String inputLine; int c; ServerSocket sSocket=new ServerSocket(8018); System。

java中的socket編程是作什么的

Socket,又稱為套接字,Socket是計(jì)算機(jī)網(wǎng)絡(luò)通信的基本的技術(shù)之一。如今大多數(shù)基于網(wǎng)絡(luò)的軟件,如瀏覽器,即時(shí)通訊工具甚至是P2P下載都是基于Socket實(shí)現(xiàn)的。本文會(huì)介紹一下基于TCP/IP的Socket編程,并且如何寫(xiě)一個(gè)客戶端/服務(wù)器程序。

方法/步驟

Java中的socket編程 下面的部分將通過(guò)一些示例講解一下如何使用socket編寫(xiě)客戶端和服務(wù)器端的程序。 注意:在接下來(lái)的示例中,我將使用基于TCP/IP協(xié)議的socket編程,因?yàn)檫@個(gè)協(xié)議遠(yuǎn)遠(yuǎn)比UDP/IP使用的要廣泛。并且所有的socket相關(guān)的類(lèi)都位于java.net包下,所以在我們進(jìn)行socket編程時(shí)需要引入這個(gè)包。

寫(xiě)入數(shù)據(jù) 接下來(lái)就是寫(xiě)入請(qǐng)求數(shù)據(jù),我們從客戶端的socket對(duì)象中得到OutputStream對(duì)象,然后寫(xiě)入數(shù)據(jù)后。很類(lèi)似文件IO的處理代碼。

打開(kāi)服務(wù)器端的socket

讀取數(shù)據(jù) 通過(guò)上面得到的socket對(duì)象獲取InputStream對(duì)象,然后安裝文件IO一樣讀取數(shù)據(jù)即可。這里我們將內(nèi)容打印出來(lái)。

使用socket實(shí)現(xiàn)一個(gè)回聲服務(wù)器,就是服務(wù)器會(huì)將客戶端發(fā)送過(guò)來(lái)的數(shù)據(jù)傳回給客戶端。B/S方向的學(xué)Socket干什么,沒(méi)有必要。

如果你需要自己做一個(gè)服務(wù)器,比如Tomcat,那么學(xué)習(xí)一下好了。

B/S Java方面,就去學(xué) J2EE相關(guān)技術(shù),JSP ,Servlet, EJB, Hibernate(iBatis),Struts/JSF,Web Services,XML,等等。

不要花精力到Socket上。socket是網(wǎng)絡(luò)通訊編程常用的,建議掌握。沒(méi)事了看看也好

再看看HTTP協(xié)議

自己做個(gè)簡(jiǎn)單的web服務(wù)器玩玩

對(duì)以后提高會(huì)有幫助相對(duì)于C++而言,JAVA在網(wǎng)絡(luò)方面的優(yōu)勢(shì)還是還行的,建議學(xué)下。

Java編程,利用Socket和ServerSocket編程。

哎。。。加分都沒(méi)有。。。

算了吧。。

服務(wù)器端:

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.ServerSocket;

import java.net.Socket;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextArea;

class wangluo1 extends JFrame implements ActionListener {

JFrame frame = new JFrame();

JPanel panel1 = new JPanel();

JPanel panel2 = new JPanel();

JTextArea text1 = new JTextArea(30, 30);

//JTextArea text2 = new JTextArea(3, 25);

JButton sent = new JButton("轉(zhuǎn)換");

InputStream in;

OutputStream out;

Socket client;

ServerSocket server;

String str;

String newStr = "";

public wangluo1() {

add("South", panel2);

add(panel1);

panel1.add(text1);

panel2.setLayout(new FlowLayout());

//panel2.add(text2);

panel2.add(sent);

sent.addActionListener(this);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

//try {

//in.close();

//out.close();

//} catch (IOException e1) {

TODO Auto-generated catch block

//e1.printStackTrace();

//}

System.exit(0);

}

});

pack();

setVisible(true);

this.setTitle("服務(wù)器:");

try {

server = new ServerSocket(10000);

client = server.accept();

in = client.getInputStream();

out = client.getOutputStream();

text1.append("已連接"+"\n");

} catch (IOException ioe) {

while (true) {

try {

byte[] buf = new byte[256];

in.read(buf);

str = new String(buf);

text1.append("客戶端說(shuō):" + "\n" +str.trim());

text1.append("\n");

} catch (IOException e) {

}

public static void main(String[] args) {

new wangluo1();

public void actionPerformed(ActionEvent e) {

try {

for(int i=str.length()-1;i>=0;i--){

newStr = newStr + str.charAt(i);

}

byte[] buf = newStr.getBytes();

//text2.setText("");

out.write(buf);

out.flush();

//text1.append("服務(wù)器說(shuō):"+"\n" + s + "\n");

} catch (IOException ie) {

客戶端:

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.Socket;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextArea;

public class wangluo2 extends JFrame implements ActionListener {

JFrame frame = new JFrame();

Socket client;

OutputStream os;

InputStream is;

String s;

JTextArea text3 = new JTextArea(30, 30);

JTextArea text4 = new JTextArea(3, 25);

JPanel panel1 = new JPanel();

JPanel panel2 = new JPanel();

JButton clientsent = new JButton("發(fā)送");

public wangluo2() {

add("South", panel2);

add(panel1);

panel1.add(text3);

panel2.setLayout(new FlowLayout());

panel2.add(text4);

panel2.add(clientsent);

pack();

setVisible(true);

this.setLocation(500, 0);

this.setTitle("客戶端");

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

//try {

//is.close();

//os.close();

//} catch (IOException e1) {

TODO Auto-generated catch block

//e1.printStackTrace();

//}

System.exit(0);

}

});

clientsent.addActionListener(this);

try {

client = new Socket("127.0.0.1", 10000);

text3.append("已連接"+"\n");

is = client.getInputStream();

os = client.getOutputStream();

} catch (IOException ioe) {

while (true) {

try {

byte[] buf = new byte[256];

is.read(buf);

String str = new String(buf);

text3.append("服務(wù)器說(shuō):" + "\n" + str.trim());

text3.append("\n");

} catch (IOException e) {

}

public static void main(String[] args) {

new wangluo2();

public void actionPerformed(ActionEvent e) {

try {

String str = text4.getText();

byte[] buf = str.getBytes();

text4.setText("");

os.write(buf);

os.flush();

text3.append("客戶端說(shuō):"+"\n" + str + "\n");

} catch (Exception ee) {

}你這個(gè)不就是TCP的例子嗎?我給你找找。

//: c15:JabberServer.java

// Very simple server that just

// echoes whatever the client sends.

// {RunByHand}

import java.io.*;

import java.net.*;

public class JabberServer {

// Choose a port outside of the range 1-1024:

public static final int PORT = 8080;

public static void main(String[] args)

throws IOException {

ServerSocket s = new ServerSocket(PORT);

System.out.println("Started: " + s);

try {

// Blocks until a connection occurs:

Socket socket = s.accept();

try {

System.out.println(

"Connection accepted: "+ socket);

BufferedReader in =

new BufferedReader(

new InputStreamReader(

socket.getInputStream()));

// Output is automatically flushed

// by PrintWriter:

PrintWriter out =

new PrintWriter(

new BufferedWriter(

new OutputStreamWriter(

socket.getOutputStream())),true);

while (true) {

String str = in.readLine();

if (str.equals("END")) break;

System.out.println("Echoing: " + str);

out.println(str);

}

// Always close the two sockets...

} finally {

System.out.println("closing...");

socket.close();

}

} finally {

s.close();

}

}

} ///:~

//: c15:JabberClient.java

// Very simple client that just sends

// lines to the server and reads lines

// that the server sends.

// {RunByHand}

import java.net.*;

import java.io.*;

public class JabberClient {

public static void main(String[] args)

throws IOException {

// Passing null to getByName() produces the

// special "Local Loopback" IP address, for

// testing on one machine w/o a network:

InetAddress addr =

InetAddress.getByName(null);

// Alternatively, you can use

// the address or name:

// InetAddress addr =

//InetAddress.getByName("127.0.0.1");

// InetAddress addr =

//InetAddress.getByName("localhost");

System.out.println("addr = " + addr);

Socket socket =

new Socket(addr, JabberServer.PORT);

// Guard everything in a try-finally to make

// sure that the socket is closed:

try {

System.out.println("socket = " + socket);

BufferedReader in =

new BufferedReader(

new InputStreamReader(

socket.getInputStream()));

// Output is automatically flushed

// by PrintWriter:

PrintWriter out =

new PrintWriter(

new BufferedWriter(

new OutputStreamWriter(

socket.getOutputStream())),true);

for(int i = 0; i < 10; i ++) {

out.println("howdy " + i);

String str = in.readLine();

System.out.println(str);

}

out.println("END");

} finally {

System.out.println("closing...");

socket.close();

}

} ///:~

總結(jié)

以上是生活随笔為你收集整理的javasocket编程(javasocket通信)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。