要实现客户端与服务器端的通讯,需要两部分C++的程序:服务器端程序代码和客户端代码。
首先是服务器端程序代码:
#include <WinSock2.h>
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib, "ws2_32.lib")
void main()
{
WSADATA wsaData
int port = 5099
char buf[] = "Server: hello, I am a server....."
if(WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("Failed to load Winsock")
return
}
//创建用于监听的套接字
SOCKET sockSrv = socket(AF_INET, SOCK_STREAM, 0)
SOCKADDR_IN addrSrv
addrSrv.sin_family = AF_INET
addrSrv.sin_port = htons(port)//1024以上的端口号
addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY)
int retVal = bind(sockSrv, (LPSOCKADDR)&addrSrv, sizeof(SOCKADDR_IN))
if(retVal == SOCKET_ERROR){
printf("Failed bind:%d\n", WSAGetLastError())
return
}
if(listen(sockSrv,10) ==SOCKET_ERROR){
printf("Listen failed:%d", WSAGetLastError())
return
}
SOCKADDR_IN addrClient
int len = sizeof(SOCKADDR)
while(1)
{
//等待客户请求到来
SOCKET sockConn = accept(sockSrv, (SOCKADDR *) &addrClient, &len)
if(sockConn == SOCKET_ERROR){
printf("Accept failed:%d", WSAGetLastError())
break
}
printf("Accept client IP:[%s]\n", inet_ntoa(addrClient.sin_addr))
//发送数据
int iSend = send(sockConn, buf, sizeof(buf) , 0)
if(iSend == SOCKET_ERROR){
printf("send failed")
break
}
char recvBuf[100]
memset(recvBuf, 0, sizeof(recvBuf))
// //接收数据
recv(sockConn, recvBuf, sizeof(recvBuf), 0)
printf("%s\n", recvBuf)
closesocket(sockConn)
}
closesocket(sockSrv)
WSACleanup()
system("pause")
}
接下来是客户端代码:
#include <WinSock2.h>#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
void main()
{
//加载套接字
WSADATA wsaData
char buff[1024]
memset(buff, 0, sizeof(buff))
if(WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("Failed to load Winsock")
return
}
SOCKADDR_IN addrSrv
addrSrv.sin_family = AF_INET
addrSrv.sin_port = htons(5099)
addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1")
//创建套接字
SOCKET sockClient = socket(AF_INET, SOCK_STREAM, 0)
if(SOCKET_ERROR == sockClient){
printf("Socket() error:%d", WSAGetLastError())
return
}
//向服务器发出连接请求
if(connect(sockClient, (struct sockaddr*)&addrSrv, sizeof(addrSrv)) == INVALID_SOCKET){
printf("Connect failed:%d", WSAGetLastError())
return
}else
{
//接收数据
recv(sockClient, buff, sizeof(buff), 0)
printf("%s\n", buff)
}
//发送数据
char buff = "hello, this is a Client...."
send(sockClient, buff, sizeof(buff), 0)
//关闭套接字
closesocket(sockClient)
WSACleanup()
}
一个server端Private Sub Command1_Click()
End
End Sub
Private Sub Command2_Click()
Winsockserver.SendData Text4.Text + ":" + textsend.Text
textget.Text = textget.Text + vbCrLf + Text4.Text + ":" + textsend.Text + " " + Str(Time)
textsend.Text = ""
End Sub
Private Sub Form_Load()
Command2.Visible = False
textsend.Visible = False
Text4.Visible = False
textget.Visible = False
Label1.Visible = False
Winsockserver.LocalPort = 1001
Winsockserver.Listen
End Sub
Private Sub textsend_Change()
'Winsockserver.SendData textsend.text
End Sub
Private Sub textsend_KeyPress(KeyAscii As Integer)
'If KeyAscii = 13 Then
'Winsockserver.SendData textsend.text
'textsend.text = ""
If KeyAscii = 13 Then
Winsockserver.SendData textsend.Text
' Text1.text = Text1.text + vbCrLf + Text4.text + ": " + Text2.text + " " + Str(Time)
textsend.Text = ""
End If
End Sub
Private Sub Timer1_Timer()
If Text4.Text = "" Then Text4.Text = "no name"
End Sub
Private Sub Winsockserver_Close()
Winsockserver.Close
End
End Sub
Private Sub Winsockserver_ConnectionRequest(ByVal requestID As Long)
Command2.Visible = True
textsend.Visible = True
textget.Visible = True
Text4.Visible = True
Label1.Visible = True
If Winsockserver.State <>sckClosed Then Winsockserver.Close
Winsockserver.Accept requestID
End Sub
Private Sub Winsockserver_DataArrival(ByVal bytesTotal As Long)
Dim tmpstr As String
Winsockserver.GetData tmpstr
'textget.text = textget + tmpstr + textsend
textget.Text = textget.Text + vbCrLf + tmpstr + " " + Str(Time)
End Sub
'(1)Command1:退出按钮;
'(2)textsend:发送数据文本框;
'(3)Winsockserver: 服务器Winsock;
'(4)textget :接收数据文本框。
一个clien
rho_b1=0:5:35rho_b2=0:0.1:35
for i=1:length(rho_b1)
smlid_err_prb(i)=ssfh_Pe(rho_b1(i))
end
for i=1:length(rho_b2)
temp=10^(rho_b2(i)/10)
if(temp>2)
theo_err_rate(i)=1/(exp(1)*temp)
else
theo_err_rate(i)=(1/2)*exp(-temp/2)
end
end
semilogy(rho_b1,smlid_err_prb,'k*',rho_b2,theo_err_rate,'k-)
在程序中调用了ssfh_Pe子函数,程序如下:
function[p]=ssfh_Pe(tho_in_dB)
rho=10^(rho_in_dB/10)
Eb=rho
if(rho>2) alpa=2/rho
else alpa=1
end
agma=sqrt(1/(2*alpha))
N=10000
for i=1:N
temp=rand
if(temp<0.5)data(i)=1
else data(i)=0
end
end
for i=1:N
if(data(i)==0)
rlc(i)=sqrt(Eb)rls(i)=0r2c(i)=0r2s(i)=0
else
rlc(i)=0rls(i)=0r2c(i)=sqrt(Eb)r2s(i)=0
end
if(rand<alpha)
rlc(i)=rlc(i)+gngauss(sgma)
rls(i)=rls(i)+gngauss(sgma)
r2c(i)=r2c(i)+gngauss(sgma)
r2s(i)=r2s(i)+gngauss(sgma)
end
end
num_of_err=0
for i=1:N
r1=rlc(i)^2+rls(i)^2
r2=r2c(i)^2+r2s(i)^2
if(r1>r2)decis=0
else decis=1
end
if(decis~=data(i))
num_off_eff=num_of_err+1
end
end
p=num_of_err/N
其中高斯分布随机变量函数gngauss程序如下
function[gsrv1,gsrv2]=gngauss(n,sgma)
if nargin==0,
m=0sgma=1
elseif nargin==1,
sgma=mm=0
end
u=rand
z=sgma*(sqrt(2*log(1/(1-u))))
u=rand
gsrv1=m+z*cos(2*pi*u)
gsrv2=m+z*sin(2*pi*u)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)