導航:首頁 > 編程語言 > socket編程應用

socket編程應用

發布時間:2022-01-18 12:39:08

① 通過java編程中socket應用,編寫一個基於c/s架構的區域網通信軟體,

//伺服器端
importjava.io.*;
importjava.net.*;
importjava.util.*;
publicclassChatServer{
booleanstarted=false;
ServerSocketss=null;
Socketsocket=null;
List<Client>clients=newArrayList<Client>();

publicstaticvoidmain(String[]args){
newChatServer().start();
}

publicvoidstart(){

try{
ss=newServerSocket(8888);
started=true;
}catch(BindExceptione){
System.out.println("埠使用中。。。。。");
System.out.println("請關掉相關程序,並重新運行伺服器!");
System.exit(0);
}catch(IOExceptione){
e.printStackTrace();
}

try{

while(started){
socket=ss.accept();
Clientc=newClient(socket);

newThread(c).start();
clients.add(c);
}
}catch(IOExceptione){
e.printStackTrace();
}

}classClientimplementsRunnable{
privateSocketsocket=null;
privateDataInputStreamdis=null;
privateDataOutputStreamdos=null;
privatebooleanbConnected=false;

publicClient(Socketsocket){
bConnected=true;
this.socket=socket;
try{
dis=newDataInputStream(socket.getInputStream());
dos=newDataOutputStream(socket.getOutputStream());
}catch(IOExceptione){
e.printStackTrace();
}
}

publicvoidsend(Stringstr){
try{
dos.writeUTF(str);
}catch(IOExceptione){
clients.remove(this);
System.out.println("對方退出了,我從List裡面去掉了!");
}
}

@Override
publicvoidrun(){
try{
while(bConnected){
Strings=dis.readUTF();
System.out.println(s);
for(inti=0;i<clients.size();i++){
Clientc=clients.get(i);
c.send(s);
}

}
}catch(EOFExceptione){
System.out.println("clientclosed!");
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
if(dis!=null)dis.close();
if(dos!=null)dos.close();
if(socket!=null)socket.close();
}catch(IOExceptione){
e.printStackTrace();
}
}

}
}
}
//客戶端
importjava.awt.event.*;
importjava.awt.*;
importjava.io.*;
importjava.net.*;
{
TextFieldtfTxt=newTextField();
TextAreataContent=newTextArea();

Socketsocket=null;
DataOutputStreamdos=null;
DataInputStreamdis=null;
booleanbConnected=false;
ThreadtRecv=newThread(newServer());

publicstaticvoidmain(String[]args){
newChatClient().launchFrame();
}

publicvoidlaunchFrame(){
setBounds(400,300,300,300);
setVisible(true);
this.setTitle("ChatClient");
this.add(tfTxt,BorderLayout.SOUTH);
this.add(taContent,BorderLayout.NORTH);
this.pack();
this.addWindowListener(newWindowAdapter(){
@Override
publicvoidwindowClosing(WindowEvente){
disconnect();
setVisible(false);
System.exit(0);
}

});
tfTxt.addActionListener(newTFMonitor());

connect();

tRecv.start();
}

publicvoidconnect(){
try{
socket=newSocket("127.0.0.1",8888);
dos=newDataOutputStream(socket.getOutputStream());
dis=newDataInputStream(this.socket.getInputStream());
System.out.println("Connected!");
bConnected=true;
}catch(UnknownHostExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
}

publicvoiddisconnect(){
try{
dos.close();
dis.close();
socket.close();
}catch(IOExceptione){
e.printStackTrace();
}

/*
try{
bConnected=false;
tRecv.join();
}catch(InterruptedExceptione){
e.printStackTrace();
}finally{
try{
dos.close();
dis.close();
socket.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
*/
}


{
@Override
publicvoidactionPerformed(ActionEvente){
Stringstr=tfTxt.getText().trim();
//taContent.setText(str);
tfTxt.setText("");
try{
//System.out.println(socket);
dos.writeUTF(str);
dos.flush();
//dos.close();
}catch(IOExceptione1){
e1.printStackTrace();
}
}

}
{

@Override
publicvoidrun(){
try{
while(bConnected){
Strings=dis.readUTF();
//System.out.println(s);
taContent.setText(taContent.getText()+s+' ');
}
}catch(IOExceptione){
System.out.println("talkover,byebye");
//e.printStackTrace();
}
}

}
}

② socket編程是什麼。

socket編程一種獨立於協議的網路編程介面,應用程序可以通過它發送或接收數據,可對其進行像對文件一樣的打開、讀寫和關閉等操作。

Socket是應用層與TCP/IP協議族通信的中間軟體抽象層,它是一組介面。在設計模式中,Socket其實就是一個門面模式,它把復雜的TCP/IP協議族隱藏在Socket介面後面,對用戶來說,一組簡單的介面就是全部,讓Socket去組織數據,以符合指定的協議。

(2)socket編程應用擴展閱讀

套接字可以看成是兩個網路應用程序進行通信時,各自通信連接中的一個端點。通信時,其中的一個網路應用程序將要傳輸的一段信息寫入它所在主機的Socket中,該Socket通過網路介面卡的傳輸介質將這段信息發送給另一台主機的Socket中,使這段信息能傳送到其他程序中。

在網路應用程序設計時,由於TCP/IP的核心內容被封裝在操作系統中,如果應用程序要使用TCP/IP,可以通過系統提供的TCP/IP的編程介面來實現。

參考資料來源:網路-socket

③ socket編程用什麼軟體

asp,jsp,php等網站編程語言除外,VB,VC,JAVA等都可以,不過SOCKET編程最好是可以寫底層的,C++最好。

④ 用Socket編程實現一個基於C/S的應用

例子一:
1 客戶端的程序

package net3;
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args){
try{
System.out.println("連接到伺服器");
Socket s=new Socket("localhost",4005);
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
String str="";
String sss="";

while(true)
{
str=b.readLine();
pw.println(str);
pw.flush();
//不理睬大小寫轉換
if(str.equalsIgnoreCase("stop"))
{
System.out.println("服務停止");
break;
}
sss=br.readLine();
System.out.println(sss);
if(sss.equalsIgnoreCase("stop"))
{
System.out.println("服務停止");
break;
}

}
b.close();
br.close();
pw.close();
}
catch(IOException e){}
}
}

2 伺服器端的程序:

package net3;
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args){
try{
System.out.println("伺服器正在啟動....");
ServerSocket ss=new ServerSocket(4005);
System.out.println("伺服器啟動,等待服務...");
Socket s=ss.accept();
BufferedReader r=new BufferedReader(new InputStreamReader(System.in));
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter bw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
String str="";
String sss="";
while(true)
{
sss=br.readLine();
System.out.println(sss);
if(sss.equalsIgnoreCase("stop"))
{
System.out.println("服務停止");
break;
}

str=r.readLine();
bw.println(str);
bw.flush();
//不理睬大小寫轉換
if(str.equalsIgnoreCase("stop"))
{
System.out.println("服務停止");
break;
}
}

br.close();
r.close();
bw.close();
}
catch(IOException e){}
}
}

例子二:
package test.socket;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class TalkMe {

public static void main(String[] args) {
System.out.println("請輸入:IP地址:");
TalkMe talk = new TalkMe();
talk.listener().start();
try {
talk.talker(new BufferedReader(new InputStreamReader(System.in)).readLine()).start();
} catch (IOException e) {
e.printStackTrace();
}
}
private Thread listener(){
return new Thread() {
public void run() {
DataInputStream dataIS = null;
String clientSay;
try {
ServerSocket server = new ServerSocket(9666);
Socket socket = server.accept();
while (true) {
dataIS = new DataInputStream(socket.getInputStream());
clientSay = dataIS.readLine();
System.out.println("Other say:" + clientSay);
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
}
private Thread talker(final String ip){
return new Thread(){
public void run() {
Socket socket = null;
InputStreamReader stdin=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(stdin);
DataInputStream dataIS = new DataInputStream(System.in);
OutputStream os;
PrintStream ps;
String say = "";
boolean flag = true;
while (flag) {
if (flag)
System.out.println("connection ....");
try {
socket = new Socket(ip, 9666);
System.out.println("connection ok.");
flag = true;
os = socket.getOutputStream();
ps = new PrintStream(os);
while (true) {
say = dataIS.readLine();
ps.println(say);
}
} catch (UnknownHostException e) {
try {
sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
} catch (IOException e) {

}
}
}
};
}

}

⑤ 利用socket編程製作一個簡單的C/S應用。

你都沒說要用什麼語言寫

閱讀全文

與socket編程應用相關的資料

熱點內容
求知課堂python2020 瀏覽:261
kafka刪除topic命令 瀏覽:759
phpsql單引號 瀏覽:86
英雄聯盟壓縮壁紙 瀏覽:452
辦公app需要什麼伺服器 瀏覽:628
安卓伺服器怎麼獲得 瀏覽:808
空調壓縮機冷媒的作用 瀏覽:781
淘寶app是以什麼為利的 瀏覽:657
java提取圖片文字 瀏覽:924
我的世界手機版指令復制命令 瀏覽:35
java判斷字元串為數字 瀏覽:926
androidrpc框架 瀏覽:490
雲伺服器essd和ssd 瀏覽:524
家用網關的加密方式 瀏覽:3
怎麼從ppt導出pdf文件 瀏覽:973
換汽車空調壓縮機軸承 瀏覽:845
平板怎麼登錄安卓端 瀏覽:197
圖像拼接計演算法 瀏覽:257
怎麼打開飢荒伺服器的本地文件夾 瀏覽:293
usb掃描槍編程 瀏覽:675