導航:首頁 > 編程語言 > java實現網路編程

java實現網路編程

發布時間:2022-01-25 14:14:29

『壹』 一個最簡單的java網路編程

你好:Socket s = new Socket("localhost", 6660);//14行

這個的話,你的沒通, 去黑窗口輸入 tenlet 127.0.0.1 6660;看看是否有結果輸出,再就是你別用localhost了,換成實際的127.0.0.1的地址,

『貳』 我要一份用java網路編程寫的點對點的兩人聊天程序(TCP和UDP)

Server端:
import java.io.*;
import java.net.*;
import java.applet.Applet;
public class TalkServer{
public static void main(String args[]) {
try{
ServerSocket server=null;
try{
server=new ServerSocket(4700);
}catch(Exception e) {
System.out.println("can not listen to:"+e);
}
Socket socket=null;
try{
socket=server.accept();
}catch(Exception e) {
System.out.println("Error."+e);
}
String line;
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter os=new PrintWriter(socket.getOutputStream());
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Client:"+is.readLine());
line=sin.readLine();
while(!line.equals("bye")){
os.println(line);
os.flush();
System.out.println("Server:"+line);
System.out.println("Client:"+is.readLine());
line=sin.readLine();
}
os.close();
is.close();
socket.close();
server.close();
}catch(Exception e){
System.out.println("Error:"+e);
}
}
}

Client端:
import java.io.*;
import java.net.*;
public class TalkClient {
public static void main(String args[]) {
try{
Socket socket=new Socket("127.0.0.1",4700);
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
PrintWriter os=new PrintWriter(socket.getOutputStream());
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String readline;
readline=sin.readLine(); //從系統標准輸入讀入一字元串
while(!readline.equals("bye")){
os.println(readline);
os.flush();
System.out.println("Client:"+readline);
System.out.println("Server:"+is.readLine());
readline=sin.readLine(); //從系統標准輸入讀入一字元串
}
os.close(); //關閉Socket輸出流
is.close(); //關閉Socket輸入流
socket.close(); //關閉Socket
}catch(Exception e) {
System.out.println("Error"+e); //出錯,則列印出錯信息
}
}
}

『叄』 java 網路編程: 如何實現客戶端與客戶端之間的之間通信

伺服器告知雙方對方的ip地址,並協調由哪一方主動連接。
如 協調結果是: 把c2的地址告訴c1,讓c1主動連接c2,讓c2打開埠等待連接。

要考慮認證問題,比如c2如何知道連接上來的是c1,而不是其他人,就需要有認證機制。
另外要考慮內網問題。由於從外部連接內網裡面的IP地址是相當繁瑣復雜的,所以需要特別的機制處理。

『肆』 Java 網路編程

import java.io.*;
import java.net.*;

public class Sendserver
{
public static int port = 3333;
public static void main(String[] args) throws IOException
{
ServerSocket s = new ServerSocket(port);
Socket d= s.accept();
System.out.println("客戶端連接成功");

DataInputStream input = new DataInputStream(d.getInputStream());
int bufferSize = 8192;
byte[] buf = new byte[bufferSize];
DataOutputStream fileOut = new DataOutputStream(new BufferedOutputStream(
new BufferedOutputStream(new FileOutputStream(
"2.mp3"))));
while (true)
{
int read = 0;
if (input != null)
read = input.read(buf);

if (read == -1)
break;

byte[] temp=new byte[read];
for(int i=0;i<read;i++)
temp[i]=buf[i];
fileOut.write(temp);
}
fileOut.close();
System.out.println("傳送完畢");

}
}

import java.io.*;
import java.net.*;

public class SendClient
{

public static void main(String[] args) throws UnknownHostException, IOException
{
try{
DataInputStream iinput = new DataInputStream(new BufferedInputStream(
new FileInputStream("1.mp3")));

InetAddress addr = InetAddress.getByName("localhost");
Socket f= new Socket(addr,3333);
OutputStream output=f.getOutputStream();

int bufferSize = 8192;
byte[] buf = new byte[bufferSize];
while (true)
{
int read = 0;
if (iinput != null)
read = iinput.read(buf);

if (read == -1)
break;

byte[] temp=new byte[read];
for(int i=0;i<read;i++)
temp[i]=buf[i];
output.write(temp);
output.flush();
}

iinput.close();
output.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}

改動相當大,你自己看看吧。

『伍』 java網路編程 實現極簡單的聊天功能

在同一區域網環境是 應該是可以的! 程序裡面只要將Beijing.java里的IP地址改為Shanghai這台機器的地址 ,Shanghai.java里的IP地址改為Beijing這台機器的地址,應該就OK

『陸』 java中如何用網路編程實現登陸功能

privatevoidpostData(Stringurl,Stringdata)throwsException{
URLurl=newURL(url);
URLConnectionurlConnection=url.openConnection();
urlConnection.setDoOutput(true);
//提交數據
try(PrintWriterpw=newPrintWriter(urlConnection.getOutputStream(),true)){
pw.write(data);
}
//獲得返回結果
try(BufferedReaderbr=newBufferedReader(
newInputStreamReader(urlConnection.getInputStream(),"UTF-8"))){
Stringline;
while((line=br.readLine())!=null){
System.out.println(line);
}
}
}

這是當初我登錄我們學校校園網的代碼(基於 JDK7),參數 url 是你登錄的起始網址(就是讓你輸入用戶名和密碼的那個,參數 data 是要提交的數據,就是形如 username=xxx&password=yyy&action=login&... 這樣的格式。

你可以自己先使用瀏覽器通過「審查元素」,然後再控制台看一下登錄的時候到底需要提交哪些欄位。

『柒』 java網路編程

bw.flush();

『捌』 什麼是Java網路編程

網路編程主要是指網路通信,實現計算機與計算機之間的對話和文件傳輸等,就像QQ,飛秋,P2P點對點傳輸等等

『玖』 JAVA網路編程需要哪些知識

基礎的知道了的話,你只要學一下java網路編程的比較基礎的東西就可以了,比如基於tcp\ip的socket編程,基於udp的socket編程.這些都比較簡單,但是能滿足你的要求了.一般的java教程的書都會涉及這些的,而且沒多少內容,只要學幾個類就行了.如果是樓主說的這么簡單的功能的話,udp方面都可以不用學的.

『拾』 java網路編程包含些什麼

java網路編程通常包括三部分TCP/IP , UDP ,URL ;socket只是其中的一個套接字。web,jsp與上面一點關系也沒有,上面是j2se的內容。下面則是j2ee內容。web是一個服務,而jsp是一種技術。實在找關系的話,web包括了jsp.

閱讀全文

與java實現網路編程相關的資料

熱點內容
伺服器旁為什麼要有電腦 瀏覽:522
什麼樣的app上買機票最便宜 瀏覽:987
安卓如何查看異常重啟 瀏覽:717
解壓音樂排名 瀏覽:386
安卓手機瀏覽器怎麼掃二維碼 瀏覽:720
通達信成本均線源碼 瀏覽:619
可以下載的解壓音頻 瀏覽:567
海賊王怎麼換伺服器 瀏覽:321
計算機上的共享文件夾映射 瀏覽:943
榮耀安裝包在文件夾哪裡 瀏覽:198
機票php源碼 瀏覽:235
linux共享mac 瀏覽:926
中國沒有國外的伺服器地址 瀏覽:761
為什麼退款伺服器連接錯誤 瀏覽:559
android簡訊存儲位置 瀏覽:977
unix網路編程卷4 瀏覽:808
找靚機app下單什麼時候發貨 瀏覽:413
android一個應用兩個進程 瀏覽:803
linux硬碟復制 瀏覽:808
php圖片伺服器搭建 瀏覽:801