导航:首页 > 配服务器 > 套接字客户端如何连接到服务器

套接字客户端如何连接到服务器

发布时间:2024-04-15 13:20:47

Ⅰ filezilla镐庝箞杩炴帴filezilla镐庝箞杩炴帴链嶅姟鍣

filezilla锛屾垜鍦ㄥ惰繛涓崭笂銆

1銆傞栧厛镓揿紑鎺у埗闱㈡澘涓镄勨渨indows阒茬伀澧欌濄2銆傜劧钖庤緭鍏モ滃厑璁哥殑搴旂敤钬濓纴镣瑰嚮鍙充笅瑙掔殑钬滃厑璁稿叾浠栧簲鐢ㄢ濄3銆傜劧钖庨夋嫨钬沧坊锷犲簲鐢ㄢ濇坊锷犫渇ilezilla钬濈殑蹇鎹锋柟寮忋4銆傛坊锷犲悗锛岃繘鍏filezilla杞浠讹纴镣瑰嚮钬灭紪杈戋濊繘鍏モ滆锷ㄦā寮忚剧疆钬濄5銆傚嬀阃夆滀娇鐢ㄤ笅𨰾塈P钬濓纴濉鍐椤师涓绘満镄処P鍦板潃鍗冲彲瑙e喅闂棰樸

auth杩炴帴涓嶅埌链嶅姟鍣锛

铡熷洜鍒嗘瀽锛

杩欐槸鐢变簬鐩鍓嶈櫄𨰾熶富链轰笉鏀鎸丗TPoverTLS镄勮繛鎺ユ柟寮忋侳TPoverTLS鏄鎸囧㈡埛绔鏄惧纺璇锋眰锛埚㈡埛绔鍙戦丄UTHTLS锻戒护锛夊笷TP浼氲瘽锷犲瘑銆傝櫄𨰾熶富链烘湇锷″櫒浼氭嫆缁濊锋眰TLS镄勮繛鎺ャ

瑙e喅鏂规硶锛

瀹㈡埛鍙浠ヨ剧疆FTP瀹㈡埛绔涓嶈佹眰浼氲瘽锷犲瘑锛屽嵆閲囩敤鏅阃氱殑FTP浼氲瘽鏂瑰纺銆

浠FileZilla杞浠朵负渚嬶纴瀹㈡埛鍦ㄧ$悊绔欑偣镄勬椂鍊欙纴钬滃姞瀵嗏濋夋嫨涓衡滃彧浣跨敤鏅阃欶TP锛堜笉瀹夊叏锛夆濓纴

淇濆瓨绔欑偣淇℃伅涔嫔悗锛屽啀娆¤繛鎺ヨ櫄𨰾熶富链烘湇锷″櫒锛屾祴璇曟垚锷熴

filezile濡备綍浣跨敤锛

瀹夎呭ソ钖庢墦寮FileZilla锛屾渶椤朵笂镄勫氨鏄杩灭▼杩炴帴锷熻兘锛

涓绘満钖嶏细sftp://+链嶅姟鍣ㄥ叕缃慽p

鐢ㄦ埛钖嶏细阒块噷浜戞带鍒跺彴涓杩灭▼杩炴帴閲岀殑璐﹀彿锛屼竴鑸涓簉oot

瀵嗙爜锛氩垰鍒氩垱寤虹殑闾d釜澶嶆潅镄勫瘑镰

绔鍙o细22

鍗曞嚮蹇阃熼摼鎺ユ寜阍锛屽嵆鍙杩炴帴鍒拌嚜宸辩殑链嶅姟鍣ㄣ

filezill浣跨敤鏁欑▼锛

filezilla镄勪娇鐢ㄦ柟娉曪细棣栧厛涓嬭浇瀹夎呭苟钖锷‵ileZilla杞浠讹绂铹跺悗灏呜瑷鍒囨崲镊充腑鏂囷绂

鎺ョ潃渚濇$偣鍑烩淓dit-Settings-Language-Chinese钬濋”骞堕吨钖锛

链钖庣偣鍑烩沧柊寤虹珯镣光濆苟杩涜岃繙绋嬭繛鎺ュ嵆鍙銆

镓嬫満浣跨敤FTP锛屽备綍璁剧疆镓嶈兘杩炴帴涓娄紶绋嫔簭锛

镓嬫満涓婄敤app寮FTP链嶅姟锛岀劧钖庝粠鐢佃剳绔阃氲繃FTP瀹㈡埛绔锛堟瘆濡侳ilezilla锛夎繛鍒版坠链虹殑IP鍦板潃鍜岀鍙e氨鍙浠ヨ块梾镓嬫満镄勬暟鎹浜嗭纴涔熷彲浠ヤ笂浼犳枃浠跺埌镓嬫満/浠庢坠链轰笅杞芥枃浠躲

镓嬫満涓婂紑FTP锛屽簲鐢ㄥ晢搴楅噷搴旇ヤ竴鎶扑竴澶ф妸銆傛垜涔嫔墠鐢‥S鏂囦欢绠$悊鍣锛屾浘缁忓緢链夊悕镄勶纴浣嗘槸鐜板湪鍙堟参链夎噧镶匡纴骞垮憡杩树竴澶у爢銆

镐庝箞浣跨敤FTP涓娄紶鏂囦欢锛

链夋椂鍊欐垜浠闇瑕佷笂浼犳枃浠跺埌铏氭嫙涓绘満鎴栬呬簯链嶅姟鍣ㄤ腑锛屼笅闱㈡潵缁椤ぇ瀹朵粙缁崭竴涓嬫庝箞镎崭綔

宸ュ叿/铡熸枡

FileZilla

鏂规硶/姝ラ

1銆佺偣鍑绘枃浠舵寜阍

2銆佺偣鍑荤珯镣圭$悊鍣

3銆佺偣鍑绘柊绔欑偣鎸夐挳

4銆佸湪鍙充晶杈揿叆瑕佽繛鎺ョ殑涓绘満钖

5銆佺鍙e彲浠ヤ笉濉榛樿ゆ槸21绔鍙

6銆佽緭鍏ftp鐢ㄦ埛钖

7銆佺偣鍑昏繛鎺ユ寜阍

8銆佽緭鍏ュ瘑镰佸悗镣瑰嚮纭瀹氭寜阍鍗冲彲

Ⅱ 链夎皝鑳界粰鎴戞濊矾钖楋纻 鍏充簬绠鍗旷殑socket缂栫▼銆

棣栧厛锛屼綘寰楀啓涓链嶅姟鍣锛涚敤涓や釜瀹㈡埛绔杩炴帴涓婃湇锷″櫒锛屽㈡埛绔阃氲繃链嶅姟鍣ㄨ繘琛屾暟鎹浜ゆ祦銆

锘轰簬TCP锛堥溃钖戣繛鎺ワ级镄凷ocket缂栫▼
链嶅姟鍣ㄧ椤哄簭锛
1. 锷犺浇濂楁帴瀛楀簱
2. 鍒涘缓濂楁帴瀛楋纸socket锛
3. 灏嗗楁帴瀛楃粦瀹氩埌涓涓链鍦板湴鍧鍜岀鍙d笂锛坆ind锛
4. 灏嗗楁帴瀛楄句负鐩戝惉妯″纺锛屽嗳澶囨帴鏀跺㈡埛璇锋眰锛坙isten锛
5. 绛夊緟瀹㈡埛璇锋眰镄勫埌𨱒ワ绂褰撹锋眰甯︽潵钖庯纴鎺ュ弹杩炴帴璇锋眰锛岃繑锲炰竴涓鏂扮殑瀵瑰簲浜庢ゆ¤繛鎺ョ殑濂楁帴瀛楋纸accept锛
6. 鐢ㄨ繑锲炵殑濂楁帴瀛楀拰瀹㈡埛绔杩涜岄氢俊锛坰end/recv锛
7. 杩斿洖锛岀瓑寰呭彟涓涓瀹㈡埛璇锋眰
8. 鍏抽棴濂楁帴瀛(closesocket)
瀹㈡埛绔绋嫔簭锛
1. 锷犺浇濂楁帴瀛楀簱
2. 鍒涘缓濂楁帴瀛(socket)
3. 钖戞湇锷″櫒鍙戦佽繛鎺ヨ锋眰锛坈onnect锛
4. 鍜屾湇锷″櫒绔杩涜岄氢俊(send/receive)
5. 鍏抽棴濂楁帴瀛(closesocket)

锘轰簬UDP锛堥溃钖戞棤杩炴帴锛夌殑socket缂栫▼
链嶅姟鍣ㄧ锛堟帴鏀剁锛夌▼搴忥细
1. 锷犺浇濂楁帴瀛楀簱
2. 鍒涘缓濂楁帴瀛楋纸socket锛
3. 灏嗗楁帴瀛楃粦瀹氩埌涓涓链鍦板湴鍧鍜岀鍙d笂锛坆ind锛
4. 绛夊緟鎺ユ敹鏁版嵁锛坮ecvfrom锛
5. 鍏抽棴濂楁帴瀛(closesocket)
瀹㈡埛绔锛埚彂阃佺锛夌▼搴
1. 锷犺浇濂楁帴瀛楀簱
2. 鍒涘缓濂楁帴瀛楋纸socket锛
3. 钖戞湇锷″櫒鍙戦佹暟鎹锛坰endto锛
4. 鍏抽棴濂楁帴瀛(closesocket)

Ⅲ UDP和Socket通信步骤

这是在网上找到的,希望对你有所帮助。
sockets(套接字)编程有三种,流式套接字(SOCK_STREAM),数据报套接字(SOCK_DGRAM),原始套接字(SOCK_RAW);

WINDOWS环境下TCP/UDP编程步骤:

1. 基于TCP的socket编程是采用的流式套接字。

在这个程序中,将两个工程添加到一个工作区。要链接一个ws2_32.lib的库文件。

服务器端编程的步骤:

1:加载套接字库,创建套接字(WSAStartup()/socket());

2:绑定套接字到一个IP地址和一个端口上(bind());

3:将套接字设置为监听模式等待连接请求(listen());

4:请求到来后,接受连接请求,返回一个新的对应于此次连接的套接字(accept());

5:用返回的套接字和客户端进行通信(send()/recv());

6:返回,等待另一连接请求;

7:关闭套接字,关闭加载的套接字库(closesocket()/WSACleanup())。

服务器端代码如下:

#include <stdio.h>

#include <Winsock2.h>

void main()

{

WORD wVersionRequested;

WSADATA wsaData;

int err;

wVersionRequested = MAKEWORD( 1, 1 );

err = WSAStartup( wVersionRequested, &wsaData );

if ( err != 0 ) {

return;

}

if ( LOBYTE( wsaData.wVersion ) != 1 ||

HIBYTE( wsaData.wVersion ) != 1 ) {

WSACleanup( );

return;

}

SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0);

SOCKADDR_IN addrSrv;

addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);

addrSrv.sin_family=AF_INET;

addrSrv.sin_port=htons(6000);

bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));

listen(sockSrv,5);

SOCKADDR_IN addrClient;

int len=sizeof(SOCKADDR);

while(1)

{

SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);

char sendBuf[50];

sprintf(sendBuf,"Welcome %s to here!",inet_ntoa(addrClient.sin_addr));

send(sockConn,sendBuf,strlen(sendBuf)+1,0);

char recvBuf[50];

recv(sockConn,recvBuf,50,0);

printf("%s\n",recvBuf);

closesocket(sockConn);

}

}

客户端编程的步骤:

1:加载套接字库,创建套接字(WSAStartup()/socket());

2:向服务器发出连接请求(connect());

3:和服务器端进行通信(send()/recv());

4:关闭套接字,关闭加载的套接字库(closesocket()/WSACleanup())。

客户端的代码如下:

#include <stdio.h>

#include <Winsock2.h>

void main()

{

WORD wVersionRequested;

WSADATA wsaData;

int err;

wVersionRequested = MAKEWORD( 1, 1 );

err = WSAStartup( wVersionRequested, &wsaData );

if ( err != 0 ) {

return;

}

if ( LOBYTE( wsaData.wVersion ) != 1 ||

HIBYTE( wsaData.wVersion ) != 1 ) {

WSACleanup( );

return;

}

SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);

SOCKADDR_IN addrSrv;

addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");

addrSrv.sin_family=AF_INET;

addrSrv.sin_port=htons(6000);

connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));

char recvBuf[50];

recv(sockClient,recvBuf,50,0);

printf("%s\n",recvBuf);

send(sockClient,"hello",strlen("hello")+1,0);

closesocket(sockClient);

WSACleanup();

}

2.基于UDP的socket编程是采用的数据报套接字。

在这个程序中,将两个工程添加到一个工作区。同时还要链接一个ws2_32.lib的库文件。

服务器端编程的步骤:

1:加载套接字库,创建套接字(WSAStartup()/socket());

2:绑定套接字到一个IP地址和一个端口上(bind());

3:等待和接收数据(sendto()/recvfrom());

4:关闭套接字,关闭加载的套接字库(closesocket()/WSACleanup())。

服务器端代码如下:

#include <winsock2.h>

#include <stdio.h>

void main()

{

WORD wVersionRequested;

WSADATA wsaData;

int err;

wVersionRequested = MAKEWORD( 1, 1 );

err = WSAStartup( wVersionRequested, &wsaData );

if ( err != 0 )

{

return;

}

if ( LOBYTE( wsaData.wVersion ) != 1 ||

HIBYTE( wsaData.wVersion ) != 1 )

{

WSACleanup( );

return;

}

SOCKET sockSrv=socket(AF_INET,SOCK_DGRAM,0);

SOCKADDR_IN addrSrv;

addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);

addrSrv.sin_family=AF_INET;

addrSrv.sin_port=htons(7003);

bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));

char recvBuf[50];

SOCKADDR addrClient;

int len=sizeof(SOCKADDR);

recvfrom(sockSrv,recvBuf,50,0,(SOCKADDR*)&addrClient,&len);

printf("%s\n",recvBuf);

closesocket(sockSrv);

WSACleanup();

}

对于基于UDP的socket客户端来说,要进行如下步骤:

1:创建一个套接字(socket);

2:向服务器发送数据(sendto);

3:关闭套接字;

代码如下:

#include <winsock2.h>

#include <stdio.h>

void main()

{

WORD wVersionRequested;

WSADATA wsaData;

int err;

wVersionRequested = MAKEWORD( 2, 2 );

err = WSAStartup( wVersionRequested, &wsaData );

if ( err != 0 ) {

return;

}

if ( LOBYTE( wsaData.wVersion ) != 2 ||

HIBYTE( wsaData.wVersion ) != 2 ) {

WSACleanup( );

return;

}

SOCKET sockClient=socket(AF_INET,SOCK_DGRAM,0);

SOCKADDR_IN addrClient;

addrClient.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");

addrClient.sin_family=AF_INET;

addrClient.sin_port=htons(8889);

SOCKADDR_IN addrSrv;

sendto(sockClient,"hi",3,0,(SOCKADDR*)&addrClient,sizeof(SOCKADDR));

}

LINUX环境下TCP/UDP编程步骤:

TCP编程步骤:

一. 服务端:

1.socket(int domain,int type,int protocol):建立套接字;

2 .bind(int sockid,struct sockaddr *addrp,socklen_t addrlen):把本机地址和端口跟上一步建立的socket绑定在一起;

3.listen(int sockid,int qsize):监听某套接字;

4.fd=accept(int sockid,struct sockaddr *callerid,socklen_t *addrlenp):等待某套接字接收信息;

5.recv(int fd,void *buf,size_t nbytes,int flags):从套接字接收数据;

6.close(fd) 和close(sockid)

二.客户端:

1. socket():建立套接字;

2.connect(int sockid,struct sockaddr *serv_addrp,socklen_t addrlen):连接到服务器;

3. send(int sockfd,const void *buf,size_t nbytes,int flags):发送数据到服务器.

4. close(sockid);

UDP编程步骤:

一,服务端:

1. socket():同上;

2. bind():同上;

3. recvfrom(int sockfd,void*buff,size_t nbytes,int flags,struct sockaddr*from,socklen_t*addrlen):在套接字口接收数据,并且记录下接收到的数据来源;一定要注意这里的参数addrlen,它不仅是函数的输出,也是函数的输入!所以要在调用该函数之前对addrlen赋值sizeof(struct sockaddr)。否则返回的地址from将会出错!

4. close(sockfd);

二. 客户端:

1. socket();同上;

2. sendto(int sockfd,const void*buff,size_t nbytes,int flags,const struct sockaddr*to,socklen_t addrlen):往指定的地址发送数据;

3. close(sockfd);

Ⅳ 在java中实现TCP协议编程中怎么传

在Java中实现TCP协议编程

ServerSocket:编写TCP网络服务程序,首先要用到java.net.ServerSocket类用以创建服务器Socket

构造方法:

ServerSocket(intport):创建绑定到特定端口的服务器套接字

ServerSocket(intport,intbacklog):利用指定的backlog(服务器忙时保持连接请求的等待客户数量),创建服务器套接字并将其绑定到指定的本地端口号。

ServerSocket(intport,intbacklog,InetAddressbindAddr):使用指定的端口、侦听backlog和要绑定到的本地IP地址创建服务器。

Socket:客户端要与服务器建立连接,必须先创建一个Socket对象

常用构造方法

Socket(Stringhost,intport):创建一个流套接字并将其连接到指定主机上的指定端口号。

Socket(InetAddressaddress,intport):创建一个流套接字并将其连接到指定IP地址的指定端口号。

服务器端程序调用ServerSocket类中的accept()方法等待客户端的连接请求,一旦accept()接收了客户端连接请求,该方法返回一个与该客户端建立了专线连接的Socket对象,不用程序去创建这个Socket对象。建立了连接的两个Socket是以IO流的方式进行数据交换的,Java提供了Socket类中的getInputStream()返回Socket的输入流对象,getOutputStream()返回Socket的输出流对象。

TCP服务器与TCP客户端间的数据的接受图示:

用TCP实现服务器与客户端的“聊天”:

实例代码:

客户端:

packagecom.hbsi.net;

importjava.net.Socket;

importjava.io.*;

publicclassTcpClient{

publicstaticvoidmain(String[]args)throwsException{

//1.建立tcp客户端socket,要确定要连接的服务器ip,port

Sockets=newSocket("192.168.49.87",9009);

//获取键盘录入

BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));

//2.通过建立的socket,获取输出流对象

//数据输出给服务器端

OutputStreamout=s.getOutputStream();

BufferedWriterbwout=newBufferedWriter(newOutputStreamWriter(out));

//获取服务器端返回的数据

//读取服务器端发过来的信息InputStreamReader()

BufferedReaderbrin=newBufferedReader(newInputStreamReader(

s.getInputStream()));

Stringline=null;

while((line=br.readLine())!=null){

if(line.equals("over"))

break;

bwout.write(line);

bwout.newLine();

bwout.flush();

Stringstr=brin.readLine();

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

}

br.close();

s.close();

}

}

服务器端:

packagecom.hbsi.net;

importjava.io.BufferedReader;

importjava.io.BufferedWriter;

importjava.io.InputStream;

importjava.io.InputStreamReader;

importjava.io.OutputStreamWriter;

importjava.net.ServerSocket;

importjava.net.Socket;

publicclassTcpServer{

publicstaticvoidmain(String[]args)throwsException{

//1.建立服务器socket

ServerSocketss=newServerSocket(9009);

//2.调用accept()

Sockets=ss.accept();

System.out.println(s.getInetAddress().getHostAddress()

+"...connection");

//读取客户的信息的输入流

InputStreamin=s.getInputStream();

BufferedReaderbrin=newBufferedReader(newInputStreamReader(in));

//向客户端发送信息输出流,服务端向客户端返回信息OutputStreamWriter()

BufferedWriterbrout=newBufferedWriter(newOutputStreamWriter(

s.getOutputStream())); Stringline=null;

while((line=brin.readLine())!=null){

System.out.println("client:"+line);

brout.write(line.toUpperCase());//服务器端收到信息后,将信息转为大写返回给客户端toUpperCase()

brout.newLine();

brout.flush();

}

s.close();

ss.close();

}

}

Ⅳ C#中TCP进行同步套接字编程,服务器和客户端的工作流程(需要代码和图)

代码来了,。。
服务器:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Reflection;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace msgServer
{
public partial class frmServer : Form
{
private Socket socket;
private IPAddress myIP;
private IPEndPoint endPoint;
private int point;
private Socket clientSocket;
private Assembly ass = null;
private System.IO.Stream stream = null;
private System.Media.SoundPlayer pl;
private string txt;
private byte[] bs = new byte[512];
public frmServer()
{
InitializeComponent();
ass = Assembly.GetExecutingAssembly();
stream = ass.GetManifestResourceStream("msgServer.msg.wav");
pl = new System.Media.SoundPlayer(stream);
}

//字体选择
private void btnFont_Click(object sender, EventArgs e)
{
FontDialog font = new FontDialog();
font.ShowEffects = true;
font.ShowColor = true;
font.MinSize = 12;
font.Font = new Font("楷体_GB2312", 18, FontStyle.Bold);
font.Color = Color.Blue;

if (font.ShowDialog() == DialogResult.OK)
{
rtxtSend.Font = font.Font;
rtxtSend.ForeColor = font.Color;
}

}

//开始监听
private void btnStart_Click(object sender, EventArgs e)
{
System.Threading.Thread th1 = new System.Threading.Thread(ToConnect);
th1.Start();
}

//去连接
private void ToConnect()
{
try
{
myIP = IPAddress.Parse(txtIP.Text.Trim());//IP
point = Convert.ToInt32(txtPoint.Text.Trim());//Point
endPoint = new IPEndPoint(myIP, point); //终端

socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(endPoint);//绑定
socket.Listen(3); //最多3人同时连接

lblStatus.Text = "服务器已经运行...";

clientSocket = socket.Accept();//等待接受
//连接上时
if (clientSocket.Connected)
{
lblStatus.Text = "已经建立连接.";

while (true)
{
try
{
clientSocket.Receive(bs, bs.Length, 0);
pl.Play();
rtxtMsg.AppendText(System.Text.Encoding.Unicode.GetString(bs));
}
catch { lblStatus.Text = "连接已经断开."; }
}
}
}
catch { txtIP.Focus(); txtIP.SelectAll(); MessageBox.Show("IP error OR Point error"); }
}

//停止监听
private void btnStop_Click(object sender, EventArgs e)
{
try
{
socket.Close(3);
lblStatus.Text = "监听已经停止.";
}
catch
{
lblStatus.Text = "目前未建立连接.";
}
}

private void frmServer_KeyPress(object sender, KeyPressEventArgs e)
{

}

private void frmServer_KeyDown(object sender, KeyEventArgs e)
{
if ((e.Control && e.KeyCode == Keys.Enter) || (e.Alt && e.KeyCode == Keys.S))
{
btnSend.PerformClick();//单击发送
}
}

//发送消息
private void btnSend_Click(object sender, EventArgs e)
{
if (rtxtSend.Text.Trim() == "")
return;
else if (rtxtSend.Text.Trim().ToLower() == "clear()")
{
rtxtMsg.Clear();
rtxtSend.Text = "";
return;
}
else if (Regex.IsMatch(rtxtSend.Text.Trim().ToLower(), @"^[zoom(]+[\d]+[)]$"))
{
string str = rtxtSend.Text.ToLower();
int size = Convert.ToInt32(str.Substring(str.LastIndexOf('(') + 1,
str.IndexOf(')') - str.LastIndexOf('(') - 1));
rtxtMsg.Font = new Font("宋体", size,FontStyle.Bold);
return;
}
else if (Regex.IsMatch(rtxtSend.Text.Trim().ToLower(), @"^[down(]+[\d]+[)]$"))
{
string str = rtxtSend.Text.ToLower();
string size = str.Substring(str.LastIndexOf('(') + 1,str.IndexOf(')') - str.LastIndexOf('(') - 1);

if (Convert.ToInt32(size) > rtxtMsg.Text.Length)
return;

}
else if (Regex.IsMatch(rtxtSend.Text.Trim().ToLower(), @"^[up(]+[\d]+[)]$"))
{
string str = rtxtSend.Text.ToLower();
string size = str.Substring(str.LastIndexOf('(') + 1,str.IndexOf(')') - str.LastIndexOf('(') - 1);

}
else if (rtxtSend.Text.Trim().ToLower() == "exit(0)")
{
this.Close();
return;
}

try
{
byte[] bs = new byte[512];
string user = null;
if (txtUser.Text.Trim() == "在此输入你的名字" || txtUser.Text.Trim() == "")
{
user = "我";
bs = Encoding.Unicode.GetBytes(string.Format("对方说:({0})\r\n{1}\r\n",DateTime.Now.ToString(), rtxtSend.Text.Trim()));
}
else
{
bs = Encoding.Unicode.GetBytes(string.Format("{0}说:({1})\r\n{2}\r\n", txtUser.Text.Trim(),DateTime.Now.ToString(), rtxtSend.Text.Trim()));
user = txtUser.Text.Trim();
}
clientSocket.Send(bs, bs.Length, 0);//发送
txt = string.Format("{0}说:({1})\r\n{2}\r\n", user, DateTime.Now.ToString(),rtxtSend.Text.Trim());
int tempLen = rtxtMsg.Text.Length;
rtxtMsg.AppendText(txt);
rtxtMsg.Select(tempLen, txt.Length);
rtxtMsg.SelectionFont = new Font("宋体", 10);
rtxtMsg.SelectionColor = Color.Red;
rtxtSend.Clear();
}
catch { MessageBox.Show("未建立连接,无法发送数据!"); }
}

//关闭Socket
private void frmServer_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
//关闭连接
socket.Close(3);
}
catch (Exception) { }
finally
{
foreach (Process p in Process.GetProcesses())
{
if (p.ProcessName == Application.ProctName)
p.Kill();
}
}
}

private void frmServer_Load(object sender, EventArgs e)
{
foreach (Process p in Process.GetProcesses())
{
if (p.ProcessName == Application.ProctName)//已经启动
{
if (p.Id != Process.GetCurrentProcess().Id)//不是当前进程
{
p.Kill();
}
}
}
}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
this.TopMost = checkBox1.Checked;
}
}
}

客户端:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace smgClient
{
public partial class frmClient : Form
{
private IPAddress IP;//ip
private EndPoint endpoint;//point
private Socket socket;//socket
private System.IO.Stream stream = null;
private System.Reflection.Assembly ass = null;
private System.Media.SoundPlayer pl = null;
private string txt;
private byte[] bs = new byte[512];

public frmClient()
{
InitializeComponent();
ass = System.Reflection.Assembly.GetExecutingAssembly();
stream = ass.GetManifestResourceStream("smgClient.msg.wav");
pl = new System.Media.SoundPlayer(stream);
}

//请求连接
private void btnRequestConnect_Click(object sender, EventArgs e)
{
System.Threading.Thread th = new System.Threading.Thread(this.ToConnect);
th.Start();
}

//连接服务器
private void ToConnect()
{
try
{
IP = IPAddress.Parse(txtIP.Text.Trim());
int point = Convert.ToInt32(txtPoint.Text.Trim());
endpoint = new IPEndPoint(IP, point);
//建立Socket
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//建立连接
socket.Connect(endpoint);
//连接成功
lblStatus.Text = "连接成功.";

while (true)
{
socket.Receive(bs, bs.Length, 0);
pl.Play();
rtxtMsg.AppendText(System.Text.Encoding.Unicode.GetString(bs));
}
}
catch (FormatException)
{ MessageBox.Show("IP error OR Point error"); }
catch (ArgumentNullException)
{ MessageBox.Show("IP error OR Point error"); }
catch (Exception)
{
lblStatus.Text = "连接失败,原因: 服务器已停止.";
}
}

//字体
private void btnFont_Click(object sender, EventArgs e)
{
FontDialog font = new FontDialog();
font.ShowEffects = true;
font.ShowColor = true;
font.MinSize = 12;
font.Font = new Font("楷体_GB2312", 18, FontStyle.Bold);
font.Color = Color.Blue;

if (font.ShowDialog() == DialogResult.OK)
{
rtxtSend.Font = font.Font;
rtxtSend.ForeColor = font.Color;
}
}

//关闭连接
private void btnCloseConnect_Click(object sender, EventArgs e)
{
try
{
socket.Close();
lblStatus.Text = "连接已经关闭.";
}
catch
{
lblStatus.Text = "目前未建立连接.";
}
}

//发送消息
private void btnSend_Click(object sender, EventArgs e)
{
if (rtxtSend.Text.Trim() == "")
return;
else if (rtxtSend.Text.Trim().ToLower() == "clear()")
{
rtxtMsg.Clear();
rtxtSend.Text = "";
return;
}
else if (Regex.IsMatch(rtxtSend.Text.Trim().ToLower(), @"^[zoom(]+[\d]+[)]$"))
{
string str = rtxtSend.Text.ToLower();
int size = Convert.ToInt32(str.Substring(str.LastIndexOf('(') + 1,
str.IndexOf(')') - str.LastIndexOf('(') - 1));
rtxtMsg.Font = new Font("宋体", size, FontStyle.Bold);
return;
}
else if (Regex.IsMatch(rtxtSend.Text.Trim().ToLower(), @"^[down(]+[\d]+[)]$"))
{
string str = rtxtSend.Text.ToLower();
string size = str.Substring(str.LastIndexOf('(') + 1, str.IndexOf(')') - str.LastIndexOf('(') - 1);

if (Convert.ToInt32(size) > rtxtMsg.Text.Length)
return;

}
else if (Regex.IsMatch(rtxtSend.Text.Trim().ToLower(), @"^[up(]+[\d]+[)]$"))
{
string str = rtxtSend.Text.ToLower();
string size = str.Substring(str.LastIndexOf('(') + 1, str.IndexOf(')') - str.LastIndexOf('(') - 1);

}
else if (rtxtSend.Text.Trim().ToLower() == "exit(0)")
{
this.Close();
return;
}

try
{
byte[] bs = new byte[512];
string user = null;
if (txtUser.Text.Trim() == "在此输入你的名字" || txtUser.Text.Trim() == "")
{
user = "我";
bs = Encoding.Unicode.GetBytes(string.Format("对方说:({0})\r\n{1}\r\n", DateTime.Now.ToString(),rtxtSend.Text.Trim()));
}
else
{
bs = Encoding.Unicode.GetBytes(string.Format("{0}说:({1})\r\n{2}\r\n", txtUser.Text.Trim(),DateTime.Now.ToString(),rtxtSend.Text.Trim()));
user = txtUser.Text.Trim();
}

socket.Send(bs, bs.Length, 0);//发送

txt = string.Format("{0}说:({1})\r\n{2}\r\n", user, DateTime.Now.ToString(), rtxtSend.Text.Trim());
int tempLen = rtxtMsg.Text.Length;
rtxtMsg.AppendText(txt);
rtxtMsg.Select(tempLen, txt.Length);
rtxtMsg.SelectionFont = new Font("宋体", 10);
rtxtMsg.SelectionColor = Color.Red;
rtxtSend.Clear();
}
catch(Exception ex)
{ MessageBox.Show("连接尚未建立!无法发送数据!" + ex.Message); }
}

//避开打开多个
private void frmClient_Load(object sender, EventArgs e)
{
foreach (Process p in Process.GetProcesses())
{
if (p.ProcessName == Application.ProctName)//有进程
{
if (p.Id != Process.GetCurrentProcess().Id)//不是当前进程
{
p.Kill();
}
}
}
}

private void frmClient_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
socket.Close();
}
catch(Exception) { }
finally
{
foreach (Process p in Process.GetProcesses())
{
if (p.ProcessName == Application.ProctName)
p.Kill();
}
}
}

private void frmClient_KeyDown(object sender, KeyEventArgs e)
{
if ((e.Control && e.KeyCode == Keys.Enter) || (e.Alt && e.KeyCode == Keys.S))
{
btnSend.PerformClick();
}
}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
this.TopMost = checkBox1.Checked;
}
}
}

Ⅵ C++ 如何使用 Socket 类向 HTTP 服务器发送数据和接收响应

客户端发送请求给服务器,服务器处理完毕以后把Socket tempSocket = (你还可以检测收到的1W8数据是3W8里面的哪一部分. 这个把发送的数据和接收的

阅读全文

与套接字客户端如何连接到服务器相关的资料

热点内容
c窗口界面编程 浏览:21
hypermill编程能做模板吗 浏览:780
计算机网络最经典的算法 浏览:626
华为思科的配置命令 浏览:869
linux设置光盘启动 浏览:359
程序员俱乐部注册 浏览:17
洗洁精鸡蛋盘解压视频 浏览:291
企业内网加密app 浏览:49
腾讯云服务器怎么设置本地网络 浏览:623
日常程序员 浏览:513
哪个pe有bit加密功能 浏览:108
rust服务器是用什么ip 浏览:692
java随机字符串生成 浏览:551
数码宝贝编程人物 浏览:390
php多用户建站系统 浏览:624
加密防盗地上栓价位 浏览:671
比泽尔压缩机下载 浏览:420
深圳压缩机制造公司 浏览:882
如何给u盘单独文件加密码 浏览:285
恒温机故障源码 浏览:419