客户端与服务端通过socket套字节连接后都会返回一个实例对象,分别保存这个对象,就相当于保存的对方的地址。不同的客户端连接到服务器,得到的对象都是不同的。服务端要发信息直接拿这个对象进行 *** 作就可以了。
很久没写了,具体名称记不起来了,思路就是这样的
字节序 是一个处理器架构特性,用于指示像整数这样的大数据类型内部的字节如何排序。
大端 (bing-endian)字节序,最大字节地址出现在最低有效字节(Least Significant Byte, LSB)上。
小端 (little-endian)字节序,最低有效字节包含最小字节地址。
不管字节如何排序,最高有效字节(Most Signification Byte, MSB)总是在左边,LSB总是在右边。
对于TCP/IP协议栈使用 大端 字节序。
有下面四个函数来转换字节序:
所谓socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄。应用程序通常通过"套接字"向网络发出请求或者应答网络请求。\x0d\以J2SDK-13为例,Socket和ServerSocket类库位于javanet包中。ServerSocket用于服务器端,Socket是建立网络连接时使用的。在连接成功时,应用程序两端都会产生一个Socket实例, *** 作这个实例,完成所需的会话。对于一个网络连接来说,套接字是平等的,并没有差别,不因为在服务器端或在客户端而产生不同级别。不管是Socket还是ServerSocket它们的工作都是通过SocketImpl类及其子类完成的。\x0d\重要的Socket API:\x0d\javanetSocket继承于javalangObject,有八个构造器,其方法并不多,下面介绍使用最频繁的三个方法,其它方法大家可以见JDK-13文档。\x0d\ Accept方法用于产生"阻塞",直到接受到一个连接,并且返回一个客户端的Socket对象实例。"阻塞"是一个术语,它使程序运行暂时"停留"在这个地方,直到一个会话产生,然后程序继续;通常"阻塞"是由循环产生的。\x0d\ getInputStream方法获得网络连接输入,同时返回一个InputStream对象实例。\x0d\ getOutputStream方法连接的另一端将得到输入,同时返回一个OutputStream对象实例。\x0d\注意:其中getInputStream和getOutputStream方法均会产生一个IOException,它必须被捕获,因为它们返回的流对象,通常都会被另一个流对象使用。\x0d\2ServerSocket类例子编辑\x0d\\x0d\package comlanbersocket;\x0d\import javaioDataInputStream;\x0d\import javaioDataOutputStream;\x0d\import javaioIOException;\x0d\import javanetServerSocket;\x0d\import javanetSocket;\x0d\public class ServerDemo {\x0d\/\x0d\ 注意:Socket的发送与接收是需要同步进行的,即客户端发送一条信息,服务器必需先接收这条信息,\x0d\ 而后才可以向客户端发送信息,否则将会有运行时出错。\x0d\ @param args\x0d\/\x0d\public static void main(String[] args) {\x0d\ServerSocket ss = null;\x0d\try {\x0d\ss = new ServerSocket(8888);\x0d\//服务器接收到客户端的数据后,创建与此客户端对话的Socket\x0d\Socket socket = ssaccept();\x0d\//用于向客户端发送数据的输出流\x0d\DataOutputStream dos = new DataOutputStream(socketgetOutputStream());\x0d\//用于接收客户端发来的数据的输入流\x0d\DataInputStream dis = new DataInputStream(socketgetInputStream());\x0d\Systemoutprintln("服务器接收到客户端的连接请求:" + disreadUTF());\x0d\//服务器向客户端发送连接成功确认信息\x0d\doswriteUTF("接受连接请求,连接成功!");\x0d\//不需要继续使用此连接时,关闭连接\x0d\socketclose();\x0d\ssclose();\x0d\} catch (IOException e) {\x0d\eprintStackTrace();\x0d\}\x0d\}\x0d\}\x0d\\x0d\3客户端的例子编辑\x0d\package comlanbersocket;\x0d\importjavaioDataInputStream;\x0d\import javaioDataOutputStream;\x0d\importjavaioIOException;\x0d\import javaioOutputStream;\x0d\import javanetSocket;\x0d\import javanetUnknownHostException;\x0d\public class ClientDemo {\x0d\/\x0d\ @param args\x0d\/\x0d\public static void main(String[] args) {\x0d\Socket socket = null;\x0d\try {\x0d\socket = new Socket("localhost",8888);\x0d\//获取输出流,用于客户端向服务器端发送数据\x0d\DataOutputStream dos = new DataOutputStream(socketgetOutputStream());\x0d\//获取输入流,用于接收服务器端发送来的数据\x0d\DataInputStream dis = new DataInputStream(socketgetInputStream());\x0d\//客户端向服务器端发送数据\x0d\doswriteUTF("我是客户端,请求连接!");\x0d\//打印出从服务器端接收到的数据\x0d\Systemoutprintln(disreadUTF());\x0d\//不需要继续使用此连接时,记得关闭哦\x0d\socketclose();\x0d\} catch (UnknownHostException e) {\x0d\eprintStackTrace();\x0d\} catch (IOException e) {\x0d\eprintStackTrace();\x0d\}\x0d\}\x0d\}
mfc只是对socket进行了一些封装,大部分人做网络编程都是用的原始的socket,比如如下接口都可以在c下进行调用\x0d\ 1socket() \x0d\ 2bind() \x0d\ 3connect() \x0d\ 4listen() \x0d\ 5accept() \x0d\ 6send() 和recv() \x0d\ 7sendto() 和recvfrom() \x0d\ 8close() 和shutdown() \x0d\ 9getpeername() \x0d\ 10gethostname() \x0d\这些接口是在Winsock2h中定义的不是在mfc中定义的,你只需要包含Winsock2h头文件和Ws2_32lib库就可以了。
//一个最简单的Socket服务端程序,不知道是否对你有用
using System;
using SystemCollectionsGeneric;
using SystemLinq;
using SystemNet;
using SystemNetSockets;
using SystemText;
using SystemThreading;
namespace SocketServer1
{
class Program
{
static bool ServiceStartFlag = false;
static Socket socket;
static Thread thread;
static void Main(string[] args)
{
socket = new Socket(AddressFamilyInterNetwork, SocketTypeStream, ProtocolTypeTcp);
IPHostEntry ieh = DnsGetHostEntry("localhost");
IPAddress localServerIP = iehAddressList[1];
IPEndPoint localIPEndPoint = new IPEndPoint(localServerIP, 8080);
socketBind(localIPEndPoint);
socketListen(600);
thread = new Thread(new ThreadStart(AcceptClient));
threadIsBackground = true;
threadStart();
ConsoleReadLine();
}
static void AcceptClient()
{
ServiceStartFlag = true;
while (ServiceStartFlag)
{
try
{
Socket newSocket = socketAccept();
string onemessge = "<cross-domain-policy><allow-access-from domain=\"" + "" + "\" to-ports=\"8080\"/></cross-domain-policy>\0";
byte[] tmpBytes = EncodingUTF8GetBytes(onemessge);
newSocketSend(tmpBytes);
Thread newThread = new Thread(new ParameterizedThreadStart(ReadMsg));
newThreadIsBackground = true;
object obj = newSocket;
newThreadStart(obj);
}
catch (SocketException ex)
{
}
}
}
static void ReadMsg(object obj)
{
Socket socket = (Socket)obj;
byte[] byteMessage = null; ;
while (ServiceStartFlag)
{
try
{
if (socketConnected)
{
byteMessage = new byte[1000];
int len = socketReceive(byteMessage);
if (len > 0)
{
string sTime = DateTimeNowToShortTimeString();
string msg = sTime + ":" + "Message from: ";
msg += socketRemoteEndPointToString() + " << " + EncodingUTF8GetString(byteMessage);
ConsoleWriteLine(msg);
byteMessage = null;
byte[] tmpBytes = EncodingUTF8GetBytes("Sended Sucessed!\0");
socketSend(tmpBytes);
}
}
}
catch (SocketException ex)
{
//ConsoleWriteLine(exMessage);
}
}
}
}
}
#include <stdioh>
#include <winsock2h>
#define MAX_SIZE 200
void main(void) {
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/ Tell the user that we could not find a usable /
/ WinSock DLL /
return;
}
/ Confirm that the WinSock DLL supports 22/
/ Note that if the DLL supports versions greater /
/ than 22 in addition to 22, it will still return /
/ 22 in wVersion since that is the version we /
/ requested /
if ( LOBYTE( wsaDatawVersion ) != 1 ||
HIBYTE( wsaDatawVersion ) != 1 ) {
/ Tell the user that we could not find a usable /
/ WinSock DLL /
WSACleanup( );
return;
}
SOCKET sockSrv = socket(AF_INET, SOCK_DGRAM, 0);
SOCKADDR_IN addrSrv;
addrSrvsin_addrS_unS_addr = htonl(INADDR_ANY);
addrSrvsin_family = AF_INET;
addrSrvsin_port = htons(6000);
bind(sockSrv, (SOCKADDR )&addrSrv, sizeof(SOCKADDR));
SOCKADDR addrClient;
char recvBuffer[MAX_SIZE];
memset(recvBuffer, 0, sizeof(recvBuffer));
int len = sizeof(SOCKADDR);
recvfrom(sockSrv, recvBuffer, sizeof(recvBuffer), 0, (SOCKADDR )&addrClient, &len);
printf("%s\n", recvBuffer);
closesocket(sockSrv);
}
#include <stdioh>
#include <winsock2h>
void main(void) {
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/ Tell the user that we could not find a usable /
/ WinSock DLL /
return;
}
/ Confirm that the WinSock DLL supports 22/
/ Note that if the DLL supports versions greater /
/ than 22 in addition to 22, it will still return /
/ 22 in wVersion since that is the version we /
/ requested /
if ( LOBYTE( wsaDatawVersion ) != 1 ||
HIBYTE( wsaDatawVersion ) != 1 ) {
/ Tell the user that we could not find a usable /
/ WinSock DLL /
WSACleanup( );
return;
}
/ The WinSock DLL is acceptable Proceed /
SOCKET sockClient = socket(AF_INET, SOCK_DGRAM, 0);
SOCKADDR_IN addrClient;
addrClientsin_addrS_unS_addr = inet_addr("127001");
addrClientsin_family = AF_INET;
addrClientsin_port = htons(6000);
sendto(sockClient, "你若此生若只如一瞬", strlen("你若此生若只如一瞬"), 0, (SOCKADDR )&addrClient, sizeof(SOCKADDR));
closesocket(sockClient);
WSACleanup();
}
以上就是关于socket网络编程全部的内容,包括:socket网络编程、python socket 编程、java中的socket是什么意思等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)