乙個簡易的聊天程式(Socket)

2021-07-02 04:37:01 字數 2692 閱讀 4735

效果圖:

服務端server**:

using system;

using system.collections.generic;

using system.componentmodel;

using system.data;

using system.drawing;

using system.io;

using system.linq;

using system.net;

using system.net.sockets;

using system.text;

using system.threading;

using system.threading.tasks;

using system.windows.forms;

namespace socket網路程式設計_server

private void btnstart_click(object sender, eventargs e)

catch

}/// /// 等待客戶端的連線 並且建立與之通訊用的socket

///

socket socketsend;

void listen(object o)//執行緒所執行的函式,如果有引數,必須是object型別

catch}}

//將遠端連線的客戶端的ip位址和socket存入集合中

dictionarydicsocket = new dictionary();

/// /// 伺服器端不停的接收客戶端發來的訊息

///

///

void recive(object o)

string str = encoding.utf8.getstring(buffer, 0, r);

showmsg(socketsend.remoteendpoint + ":" + str);

}catch }}

void showmsg(string str)

private void form1_load(object sender, eventargs e)

/// /// 伺服器給客戶端傳送訊息

///

///

///

private void btnsend_click(object sender, eventargs e)

catch {}}

/// /// 選擇要傳送的檔案

///

///

///

private void btnselect_click(object sender, eventargs e)

private void btnsendfile_click(object sender, eventargs e)

}/// /// 傳送震動

///

///

///

private void btnzd_click(object sender, eventargs e)}}

客戶端client**:

using system;

using system.collections.generic;

using system.componentmodel;

using system.data;

using system.drawing;

using system.io;

using system.linq;

using system.net;

using system.net.sockets;

using system.text;

using system.threading;

using system.threading.tasks;

using system.windows.forms;

namespace socket網路程式設計_client

socket socketsend;

private void btnstart_click(object sender, eventargs e)

catch

}/// /// 不停的接收伺服器發來的訊息

///

void recive()

//先拿到第一位,判斷傳送的是否文字訊息

if (buffer[0] == 0)

if (buffer[0] == 1)

messagebox.show("儲存成功 ");

}if (buffer[0] == 2)

}catch }}

/// /// 震動

///

void zd()

}void showmsg(string str)

/// /// 客戶端給伺服器傳送訊息

///

///

///

private void btnsend_click(object sender, eventargs e)

private void form1_load(object sender, eventargs e)}}

非同步Socket編寫的乙個簡易的聊天室

非同步socket的一點心得,注釋的很完善。希望對還不太熟悉socket的有點幫助,哪怕是一點點就可以了。不說了,先截圖吧 設定終結點,並用非同步來接收傳過來的socket物件 public void getsocket public void shu iasyncresult e 這裡建立代表是用...

socket例項C語言 乙個簡單的聊天程式

我們老師讓寫乙個簡單的聊天軟體,並且實現不同機子之間的通訊,我用的是socket程式設計。不廢話多說了,先附上 伺服器端server.c include include include include include include include include include include d...

socket建立的簡易聊天器

1 import socket23 4def send message udp socket 5 傳送訊息 6 send data input 請輸入要傳送的資訊 7 dest ip input 8 dest port int input 請輸入目標的埠 9 udp socket.sendto se...