Unity Socket 互動程式設計
通過網上的資料做了有關Socket的第一個Demo,雖然不是很成熟,但個人感覺已經相對比較完整了。各型別資料的傳輸接受都有,其中也做了float型別的(因為我感覺做Unity應該用的著)。
功能方面,為了測試多使用者互動,我在Demo上開啟了4個Socket 4個執行緒接受資料。實現了服務端通知使用者進入 離開功能。
真機上,需要加上字型檔,要不無法顯示中文。
工程目錄:
下面上程式碼:
客戶端程式碼:
[csharp] view plaincopyprint?- using UnityEngine;
- using System;
- using System.Collections;
- using LSocket.Net;
- using LSocket.Type;
- using LSocket.cmd;
- publicclass SocketDemo : MonoBehaviour
- {
- public UnitySocket[] socket;
- public String[] textAreaString;
- public String[] textFieldString;
- public GUISkin mySkin;
- // Use this for initialization
- void Start()
- {
- socket = new UnitySocket[4];
- textAreaString = new String[12];
- for (int i = 0; i < 12; i++)
- {
- textAreaString[i] = "";
- }
- textFieldString = new String[4];
- for(int i=0;i<4;i++){
- textFieldString[i] =
- }
- }
- // Update is called once per frame
- void Update()
- {
- }
- void OnGUI()
- {
- GUI.skin = mySkin;
- for (int i = 0; i < 4; i++)
- {
- String s = textAreaString[i * 3] + "\n" + textAreaString[i * 3 + 1] + "\n" + textAreaString[i * 3 + 2];
- GUI.TextArea(new Rect(i % 2 * Screen.width / 2, i / 2 * (Screen.height / 2) + 50, 100, 60), s);
- textFieldString[i] = GUI.TextField(new Rect(i % 2 * Screen.width / 2+50, i / 2 * (Screen.height / 2), 100, 20), textFieldString[i]);
- if (GUI.Button(new Rect(i % 2 * Screen.width / 2, i / 2 * (Screen.height / 2), 40, 20), "連線"))
- {
- socket[i] = null;
- socket[i] = new UnitySocket();
- socket[i].SocketConnection("192.168.0.8", 10000, this, i);
- socket[i].DoLogin(textFieldString[i]);
- }
- elseif (GUI.Button(new Rect(i % 2 * Screen.width / 2, i / 2 *( Screen.height / 2) + 25, 40, 20), "關閉"))
- {
- if (socket[i] != null)
- {
- socket[i].close();
- socket[i] = null;
- }
- }
- }
- if (GUI.Button(new Rect(Screen.width - 60, Screen.height - 30, 60, 30), "退出")) {
- Application.Quit();
- }
- }
- }
using UnityEngine;
using System;
using System.Collections;
using LSocket.Net;
using LSocket.Type;
using LSocket.cmd;
public class SocketDemo : MonoBehaviour
{
public UnitySocket[] socket;
public String[] textAreaString;
public String[] textFieldString;
public GUISkin mySkin;
// Use this for initialization
void Start()
{
socket = new UnitySocket[4];
textAreaString = new String[12];
for (int i = 0; i < 12; i++)
{
textAreaString[i] = "";
}
textFieldString = new String[4];
for(int i=0;i<4;i++){
textFieldString[i] = "";
}
}
// Update is called once per frame
void Update()
{
}
void OnGUI()
{
GUI.skin = mySkin;
for (int i = 0; i < 4; i++)
{
String s = textAreaString[i * 3] + "\n" + textAreaString[i * 3 + 1] + "\n" + textAreaString[i * 3 + 2];
GUI.TextArea(new Rect(i % 2 * Screen.width / 2, i / 2 * (Screen.height / 2) + 50, 100, 60), s);
textFieldString[i] = GUI.TextField(new Rect(i % 2 * Screen.width / 2+50, i / 2 * (Screen.height / 2), 100, 20), textFieldString[i]);
if (GUI.Button(new Rect(i % 2 * Screen.width / 2, i / 2 * (Screen.height / 2), 40, 20), "連線"))
{
socket[i] = null;
socket[i] = new UnitySocket();
socket[i].SocketConnection("192.168.0.8", 10000, this, i);
socket[i].DoLogin(textFieldString[i]);
}
else if (GUI.Button(new Rect(i % 2 * Screen.width / 2, i / 2 *( Screen.height / 2) + 25, 40, 20), "關閉"))
{
if (socket[i] != null)
{
socket[i].close();
socket[i] = null;
}
}
}
if (GUI.Button(new Rect(Screen.width - 60, Screen.height - 30, 60, 30), "退出")) {
Application.Quit();
}
}
}
[csharp]
view plaincopyprint?
- namespace LSocket.Net
- { /**
- *
- * @author feng俠,qq:313785443
- * @date 2010-12-23
- *
- */
- // 描 述:封裝c# socket資料傳輸協議
- using UnityEngine;
- using System;
- using System.Net.Sockets;
- using System.Net;
- using System.Collections;
- using System.Text;
- using System.Threading;
- using LSocket.Type;
- using LSocket.cmd;
- class SocketThread
- {
- UnitySocket socket;
- SocketDemo demo;
- int idx;
- public SocketThread(UnitySocket socket, SocketDemo demo, int idx)
- {
- this.socket = socket;
- this.demo = demo;
- this.idx = idx;
- }
- publicvoid run()
- {
- while (true)
- {
- try
- {
- String s = socket.ReceiveString();
- demo.textAreaString[idx * 3] = demo.textAreaString[idx * 3 + 1];
- demo.textAreaString[idx * 3 + 1] = demo.textAreaString[idx * 3 + 2];
- demo.textAreaString[idx * 3 + 2] = s;
- MonoBehaviour.print(s + " " + idx);
- }
- catch (Exception e)
- {
- MonoBehaviour.print(e.ToString());
- socket.t.Abort();
- }
- }
- }
- }
- publicclass UnitySocket
- {
- public Socket mSocket = null;
- public Thread t=null;
- private SocketThread st=null;
- public SocketDemo demo=null;
- public UnitySocket()
- {
- }
- publicvoid SocketConnection(string LocalIP, int LocalPort,SocketDemo demo,int idx)
- {
- this.demo=demo;
- mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- try
- {
- IPAddress ip = IPAddress.Parse(LocalIP);
- IPEndPoint ipe = new IPEndPoint(ip, LocalPort);
- mSocket.Connect(ipe);
- st =new SocketThread(this, demo, idx);
- t = new Thread(new ThreadStart(st.run));
- t.Start();
- }
- catch (Exception e)
- {
- MonoBehaviour.print(e.ToString());
- }
- }
- publicvoid close(){
- mSocket.Close(0);
- mSocket=null;
- }
- publicvoid DoLogin(String userName){
- try
- {
- Send(CommandID.LOGIN);
- Send(userName);
- }catch(Exception e){
- MonoBehaviour.print(e.ToString());
- }
- }
- publicvoid Send(float data){
- byte[] longth = TypeConvert.getBytes(data, true);
- mSocket.Send(longth);
- }
- publicfloat ReceiveFloat()
- {
- byte[] recvBytes = newbyte[4];
- mSocket.Receive(recvBytes, 4, 0);//從伺服器端接受返回資訊
- float data = TypeConvert.getFloat(recvBytes, true);
- return data;
- }
- publicvoid Send(short data)
- {
- byte[] longth=TypeConvert.getBytes(data,true);
- mSocket.Send(longth);
- }
- publicvoid Send(long data)
- {
- byte[] longth=TypeConvert.getBytes(data,true);
- mSocket.Send(longth);
- }
- publicvoid Send(int data)
- {
- byte[] longth=TypeConvert.getBytes(data,true);
- mSocket.Send(longth);
- }
- publicvoid Send(string data)
- {
- byte[] longth=Encoding.UTF8.GetBytes(data);
- Send(longth.Length);
- mSocket.Send(longth);
- }
- publicshort ReceiveShort()
- {
- byte[] recvBytes = newbyte[2];
- mSocket.Receive(recvBytes,2,0);//從伺服器端接受返回資訊
- short data=TypeConvert.getShort(recvBytes,true);
- return data;
- }
- publicint ReceiveInt()
- {
- byte[] recvBytes = newbyte[4];
- mSocket.Receive(recvBytes,4,0);//從伺服器端接受返回資訊
- int data=TypeConvert.getInt(recvBytes,true);
- return data;
- }
- publiclong ReceiveLong()
- {
- byte[] recvBytes = newbyte[8];
- mSocket.Receive(recvBytes,8,0);//從伺服器端接受返回資訊
- long data=TypeConvert.getLong(recvBytes,true);
- return data;
- }
- public String ReceiveString()
- {
- int length = ReceiveInt();
- MonoBehaviour.print("Stringlen="+length);
- byte[] recvBytes = newbyte[length];
- mSocket.Receive(recvBytes,length,0);//從伺服器端接受返回資訊
- String data = Encoding.UTF8.GetString(recvBytes);
- return data;
- }
- }
- }
namespace LSocket.Net
{ /**
*
* @author feng俠,qq:313785443
* @date 2010-12-23
*
*/
// 描 述:封裝c# socket資料傳輸協議
using UnityEngine;
using System;
using System.Net.Sockets;
using System.Net;
using System.Collections;
using System.Text;
using System.Threading;
using LSocket.Type;
using LSocket.cmd;
class SocketThread
{
UnitySocket socket;
SocketDemo demo;
int idx;
public SocketThread(UnitySocket socket, SocketDemo demo, int idx)
{
this.socket = socket;
this.demo = demo;
this.idx = idx;
}
public void run()
{
while (true)
{
try
{
String s = socket.ReceiveString();
demo.textAreaString[idx * 3] = demo.textAreaString[idx * 3 + 1];
demo.textAreaString[idx * 3 + 1] = demo.textAreaString[idx * 3 + 2];
demo.textAreaString[idx * 3 + 2] = s;
MonoBehaviour.print(s + " " + idx);
}
catch (Exception e)
{
MonoBehaviour.print(e.ToString());
socket.t.Abort();
}
}
}
}
public class UnitySocket
{
public Socket mSocket = null;
public Thread t=null;
private SocketThread st=null;
public SocketDemo demo=null;
public UnitySocket()
{
}
public void SocketConnection(string LocalIP, int LocalPort,SocketDemo demo,int idx)
{
this.demo=demo;
mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
IPAddress ip = IPAddress.Parse(LocalIP);
IPEndPoint ipe = new IPEndPoint(ip, LocalPort);
mSocket.Connect(ipe);
st =new SocketThread(this, demo, idx);
t = new Thread(new ThreadStart(st.run));
t.Start();
}
catch (Exception e)
{
MonoBehaviour.print(e.ToString());
}
}
public void close(){
mSocket.Close(0);
mSocket=null;
}
public void DoLogin(String userName){
try
{
Send(CommandID.LOGIN);
Send(userName);
}catch(Exception e){
MonoBehaviour.print(e.ToString());
}
}
public void Send(float data){
byte[] longth = TypeConvert.getBytes(data, true);
mSocket.Send(longth);
}
public float ReceiveFloat()
{
byte[] recvBytes = new byte[4];
mSocket.Receive(recvBytes, 4, 0);//從伺服器端接受返回資訊
float data = TypeConvert.getFloat(recvBytes, true);
return data;
}
public void Send(short data)
{
byte[] longth=TypeConvert.getBytes(data,true);
mSocket.Send(longth);
}
public void Send(long data)
{
byte[] longth=TypeConvert.getBytes(data,true);
mSocket.Send(longth);
}
public void Send(int data)
{
byte[] longth=TypeConvert.getBytes(data,true);
mSocket.Send(longth);
}
public void Send(string data)
{
byte[] longth=Encoding.UTF8.GetBytes(data);
Send(longth.Length);
mSocket.Send(longth);
}
public short ReceiveShort()
{
byte[] recvBytes = new byte[2];
mSocket.Receive(recvBytes,2,0);//從伺服器端接受返回資訊
short data=TypeConvert.getShort(recvBytes,true);
return data;
}
public int ReceiveInt()
{
byte[] recvBytes = new byte[4];
mSocket.Receive(recvBytes,4,0);//從伺服器端接受返回資訊
int data=TypeConvert.getInt(recvBytes,true);
return data;
}
public long ReceiveLong()
{
byte[] recvBytes = new byte[8];
mSocket.Receive(recvBytes,8,0);//從伺服器端接受返回資訊
long data=TypeConvert.getLong(recvBytes,true);
return data;
}
public String ReceiveString()
{
int length = ReceiveInt();
MonoBehaviour.print("Stringlen="+length);
byte[] recvBytes = new byte[length];
mSocket.Receive(recvBytes,length,0);//從伺服器端接受返回資訊
String data = Encoding.UTF8.GetString(recvBytes);
return data;
}
}
}
[csharp]
view plaincopyprint?
- namespace LSocket.Type
- {
- using UnityEngine;
- using System.Collections;
- publicclass TypeConvert
- {
- public TypeConvert()
- {
- }
- publicstaticbyte[] getBytes(float s,bool asc){
- int buf = (int)(s * 100);
- return getBytes(buf,asc);
- }
- publicstaticfloat getFloat(byte[] buf,bool asc){
- int i=getInt(buf,asc);
- float s=(float)i;
- return s/100;
- }
- publicstaticbyte[] getBytes(short s, bool asc)
- {
- byte[] buf = newbyte[2];
- if (asc)
- {
- for (int i = buf.Length - 1; i >= 0; i--)
- {
- buf[i] = (byte)(s & 0x00ff);
- s >>= 8;
- }
- }
- else
- {
- for (int i = 0; i < buf.Length; i++)
- {
- buf[i] = (byte)(s & 0x00ff);
- s >>= 8;
- }
- }
- return buf;
- }
- publicstaticbyte[] getBytes(int s, bool asc)
- {
- byte[] buf = newbyte[4];
- if (asc)
- for (int i = buf.Length - 1; i >= 0; i--)
- {
- buf[i] = (byte)(s & 0x000000ff);
- s >>= 8;
- }
- else
- for (int i = 0; i < buf.Length; i++)
- {
- buf[i] = (byte)(s & 0x000000ff);
- s >>= 8;
- }
- return buf;
- }
- publicstaticbyte[] getBytes(long s, bool asc)
- {
- byte[] buf = newbyte[8];
- if (asc)
- for (int i = buf.Length - 1; i >= 0; i--)
- {
- buf[i] = (byte)(s & 0x00000000000000ff);
- s >>= 8;
- }
- else
- for (int i = 0; i < buf.Length; i++)
- {
- buf[i] = (byte)(s & 0x00000000000000ff);
- s >>= 8;
- }
- return buf;
- }
- publicstaticshort getShort(byte[] buf, bool asc)
- {
- if (buf == null)
- {
- //throw new IllegalArgumentException("byte array is null!");
- }
- if (buf.Length > 2)
- {
- //throw new IllegalArgumentException("byte array size > 2 !");
- }
- short r = 0;
- if (!asc)
- for (int i = buf.Length - 1; i >= 0; i--)
- {
- r <<= 8;
- r |= (short)(buf[i] & 0x00ff);
- }
- else
- for (int i = 0; i < buf.Length; i++)
- {
- r <<= 8;
- r |= (short)(buf[i] & 0x00ff);
- }
- return r;
- }
- publicstaticint getInt(byte[] buf, bool asc)
- {
- if (buf == null)
- {
- // throw new IllegalArgumentException("byte array is null!");
- }
- if (buf.Length > 4)
- {
- //throw new IllegalArgumentException("byte array size > 4 !");
- }
- int r = 0;
- if (!asc)
- for (int i = buf.Length - 1; i >= 0; i--)
- {
- r <<= 8;
- r |= (buf[i] & 0x000000ff);
- }
- else
- for (int i = 0; i < buf.Length; i++)
- {
- r <<= 8;
- r |= (buf[i] & 0x000000ff);
- }
- return r;
- }
- publicstaticlong getLong(byte[] buf, bool asc)
- {
- if (buf == null)
- {
- //throw new IllegalArgumentException("byte array is null!");
- }
- if (buf.Length > 8)
- {
- //throw new IllegalArgumentException("byte array size > 8 !");
- }
- long r = 0;
- if (!asc)
- for (int i = buf.Length - 1; i >= 0; i--)
- {
- r <<= 8;
- r |= (buf[i] & 0x00000000000000ff);
- }
- else
- for (int i = 0; i < buf.Length; i++)
- {
- r <<= 8;
- r |= (buf[i] & 0x00000000000000ff);
- }
- return r;
- }
- }
- }
namespace LSocket.Type
{
using UnityEngine;
using System.Collections;
public class TypeConvert
{
public TypeConvert()
{
}
public static byte[] getBytes(float s,bool asc){
int buf = (int)(s * 100);
return getBytes(buf,asc);
}
public static float getFloat(byte[] buf,bool asc){
int i=getInt(buf,asc);
float s=(float)i;
return s/100;
}
public static byte[] getBytes(short s, bool asc)
{
byte[] buf = new byte[2];
if (asc)
{
for (int i = buf.Length - 1; i >= 0; i--)
{
buf[i] = (byte)(s & 0x00ff);
s >>= 8;
}
}
else
{
for (int i = 0; i < buf.Length; i++)
{
buf[i] = (byte)(s & 0x00ff);
s >>= 8;
}
}
return buf;
}
public static byte[] getBytes(int s, bool asc)
{
byte[] buf = new byte[4];
if (asc)
for (int i = buf.Length - 1; i >= 0; i--)
{
buf[i] = (byte)(s & 0x000000ff);
s >>= 8;
}
else
for (int i = 0; i < buf.Length; i++)
{
buf[i] = (byte)(s & 0x000000ff);
s >>= 8;
}
return buf;
}
public static byte[] getBytes(long s, bool asc)
{
byte[] buf = new byte[8];
if (asc)
for (int i = buf.Length - 1; i >= 0; i--)
{
buf[i] = (byte)(s & 0x00000000000000ff);
s >>= 8;
}
else
for (int i = 0; i < buf.Length; i++)
{
buf[i] = (byte)(s & 0x00000000000000ff);
s >>= 8;
}
return buf;
}
public static short getShort(byte[] buf, bool asc)
{
if (buf == null)
{
//throw new IllegalArgumentException("byte array is null!");
}
if (buf.Length > 2)
{
//throw new IllegalArgumentException("byte array size > 2 !");
}
short r = 0;
if (!asc)
for (int i = buf.Length - 1; i >= 0; i--)
{
r <<= 8;
r |= (short)(buf[i] & 0x00ff);
}
else
for (int i = 0; i < buf.Length; i++)
{
r <<= 8;
r |= (short)(buf[i] & 0x00ff);
}
return r;
}
public static int getInt(byte[] buf, bool asc)
{
if (buf == null)
{
// throw new IllegalArgumentException("byte array is null!");
}
if (buf.Length > 4)
{
//throw new IllegalArgumentException("byte array size > 4 !");
}
int r = 0;
if (!asc)
for (int i = buf.Length - 1; i >= 0; i--)
{
r <<= 8;
r |= (buf[i] & 0x000000ff);
}
else
for (int i = 0; i < buf.Length; i++)
{
r <<= 8;
r |= (buf[i] & 0x000000ff);
}
return r;
}
public static long getLong(byte[] buf, bool asc)
{
if (buf == null)
{
//throw new IllegalArgumentException("byte array is null!");
}
if (buf.Length > 8)
{
//throw new IllegalArgumentException("byte array size > 8 !");
}
long r = 0;
if (!asc)
for (int i = buf.Length - 1; i >= 0; i--)
{
r <<= 8;
r |= (buf[i] & 0x00000000000000ff);
}
else
for (int i = 0; i < buf.Length; i++)
{
r <<= 8;
r |= (buf[i] & 0x00000000000000ff);
}
return r;
}
}
}
[csharp]
view plaincopyprint?
- namespace LSocket.cmd
- {
- publicclass CommandID
- {
- /** 登陸訊息命令 **/
- publicstaticint LOGIN = 1001;
- }
- }
namespace LSocket.cmd
{
public class CommandID
{
/** 登陸訊息命令 **/
public static int LOGIN = 1001;
}
}
伺服器端程式碼 Java:
- package com.unity.socket;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.HashMap;
- publicclass SocketServer {
- private HashMap<String,User> userMap;
- public SocketServer(){
- userMap=new HashMap<String, User>();
- }
- publicstaticvoid main(String[] args){
- new SocketServer().startServer();
- }
- publicvoid startServer()
- {
- try
- {
- ServerSocket serverSocket = new ServerSocket(10000);
- System.out.println("伺服器開啟");
- while (true){
- Socket socket = serverSocket.accept();
- System.out.println("有使用者登陸進來了");
- new UserThread(socket,userMap).start();
- }
- }catch (Exception e){
- System.out.println("伺服器出現異常!" + e);
- }
- }
- }
package com.unity.socket;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
public class SocketServer {
private HashMap<String,User> userMap;
public SocketServer(){
userMap=new HashMap<String, User>();
}
public static void main(String[] args){
new SocketServer().startServer();
}
public void startServer()
{
try
{
ServerSocket serverSocket = new ServerSocket(10000);
System.out.println("伺服器開啟");
while (true){
Socket socket = serverSocket.accept();
System.out.println("有使用者登陸進來了");
new UserThread(socket,userMap).start();
}
}catch (Exception e){
System.out.println("伺服器出現異常!" + e);
}
}
}
[java]
view plaincopyprint?
- package com.unity.socket;
- import java.net.Socket;
- publicclass User {
- private String name;
- private Socket socket;
- public String getName() {
- return name;
- }
- publicvoid setName(String name) {
- this.name = name;
- }
- public Socket getSocket() {
- return socket;
- }
- publicvoid setSocket(Socket socket) {
- this.socket = socket;
- }
- }
package com.unity.socket;
import java.net.Socket;
public class User {
private String name;
private Socket socket;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
}
[java]
view plaincopyprint?
- package com.unity.socket;
- import java.io.*;
- import java.net.Socket;
- import java.util.Iterator;
- import java.io.ByteArrayOutputStream;
- import java.io.DataOutputStream;
- import java.util.HashMap;
- publicclass UserThread extends Thread{
- /** 錯誤訊息命令 **/
- publicstaticfinalint ERROR = 0;
- /** 登陸訊息命令 **/
- publicstaticfinalint LOGIN = 1001;
- private Socket socket;
- private HashMap<String,User> userMap;
- private User user;
- private ByteArrayOutputStream byteOutput;
- private DataOutputStream output;
- private DataInputStream input;
- public UserThread(Socket socket,HashMap<String,User> userMap){
- this.socket=socket;
- this.userMap=userMap;
- }
- //重新初始化2個新的output
- privatevoid initOutput()
- {
- byteOutput = new ByteArrayOutputStream();
- output = new DataOutputStream(byteOutput);
- }
- publicvoid sendAllUser(byte[] bytes) throws Exception
- {
- for(Iterator<User> it = userMap.values().iterator(); it.hasNext();)
- {
- sendMessage(it.next().getSocket(),bytes);
- }
- }
- publicvoid sendMessage(Socket socket,byte[] bytes) throws Exception
- {
- DataOutputStream dataOutput = new DataOutputStream(socket.getOutputStream());
- dataOutput.write(bytes);
- dataOutput.flush();
- }
- publicshort readShort()throws IOException{
- byte[] buf=newbyte[2];
- input.read(buf);
- return ConvertType.getShort(buf,true);
- }
- publicint readInt()throws IOException{
- byte[] buf=newbyte[4];
- input.read(buf);
- return ConvertType.getInt(buf, true);
- }
- publiclong readLong()throws IOException{
- byte[] buf=newbyte[8];
- input.read(buf);
- return ConvertType.getLong(buf, true);
- }
- publicfloat readFloat()throws IOException{
- byte[] buf=newbyte[4];
- input.read(buf);
- return ConvertType.getFloat(buf, true);
- }
- public String readString()throws IOException{
- int length=readInt();
- byte[] buf=newbyte[length];
- input.read(buf);
- return
相關推薦
Unity Socket 互動程式設計
這幾天研究了下Socket互動。 通過網上的資料做了有關Socket的第一個Demo,雖然不是很成熟,但個人感覺已經相對比較完整了。各型別資料的傳輸接受都有,其中也做了float型別的(因為我感覺做Unity應該用的著)。 功能方面,為了測試多使用者互
Unity Socket網路程式設計(TCP) 簡單例子-1
開發工具:Visual Studio、Unity 開發語言:C# 【伺服器端】 在VS建立一個C#空專案 程式碼如下: using System; using Syste
Unity Socket網路程式設計(TCP)
開發工具:Visual Studio、Unity 開發語言:C# 【伺服器端】 在VS建立一個C#空專案 程式碼如下: using System; using System.Collections.G
java:socket 網路程式設計
socket的通俗解釋: 套接字=主機+埠號。兩個東西配在一起,叫做“配套”。 另外“套”也有對應的意思,它可以把網路上的兩個應用對應起來,所以用“套”。 它是用來與另一個應用連線的,所以用“接”。 又因為它是一小段資料,很小一小段,所以叫“字”。 “套接字",就是一小段用來將網路個兩個應用
[Socket網路程式設計]一個封鎖操作被對 WSACancelBlockingCall 的呼叫中斷。
原文地址:http://www.cnblogs.com/xiwang/archive/2012/10/25/2740114.html記錄在此,方便查閱。 C#中在使用UDPClient迴圈監聽埠,在斷開UPDClient的時候,使用try...catch捕獲了異常,System.NET.Socket
Python_day6:socket網路程式設計
一、socket socket即套接字,用於描述IP地址和埠,是一個通訊鏈的控制代碼,應用程式通常通過"套接字"向網路發出請求或者應答網路請求。 最簡單的socket,一次 1 import socket 2 server = socket.socket() #獲得例項
python------Socket網路程式設計(二)粘包問題
一.socket網路程式設計 粘包:服務端兩次傳送指令在一起,它會把兩次傳送內容合在一起傳送,稱為粘包,從而出現錯誤。 解決方法:(比較low的方法) 有些需要實時更新的,用sleep有延遲,不能這樣解決問題。 解決方法之高階方法: 客戶端: 二.傳送檔案 ftp s
門禁系統socket通訊程式設計
最近遇到一個socke udp協議通訊的需求,而且是16進位制資料接收。這樣在傳輸引數的時候老是提示引數錯誤,因為計算機是不能直接傳輸16進位制的,會自行轉換,所有以下程式碼非常完美的解決我的問題,同時也讓我認識到並不是所有socket都是需要一個客戶端和服務端程式碼 <?php &nbs
Socket網路程式設計進階與實戰資源分享
Socket網路程式設計進階與實戰資源分享 Socket網路程式設計進階與實戰資源分享 獲取資源新增qq+2100776785 獲取資源新增qq+2100776785 第1章 課程介紹 本章將從軟體測試的起源與發展、測試行業的現狀及職業生涯規劃等整體做介紹。 第2章 軟體測試工程師必
python 協程及socket網路程式設計
協程 什麼是協程 協程,英文Coroutines,是一種比執行緒更加輕量級的存在。正如一個程序可以擁有多個執行緒一樣,一個執行緒也可以擁有多個協程。 最重要的是,協程不是被作業系統核心所管理,而完全是由程式所控制(也就是在使用者態執行)。 這樣帶來的好處就是效能得到了很大的提升,不會
Python Socket網路程式設計(一)初識Socket和Socket初步使用
目錄 前言 網路程式設計 實質 IP地址和埠 資料傳輸協議 協議 Socket
Python Socket網路程式設計(二)區域網內和區域網與廣域網的持續通訊
目錄 前言 IP地址 簡介 公有IP 私有IP 區域網之間網路通訊 前提 功能描述
銀行業務系統(c/s架構、socket網路程式設計、多執行緒)
1、功能要求 包括兩類使用者:管理人員和普通使用者(本文只寫了普通使用者程式) 普通使用者功能:登入登出、存取款、轉賬、查詢餘額 2、技術要求 要求用到多程序多執行緒 要求同時允許多個使用者操作(因為沒有註冊賬號功能,且只初始化了兩個賬號資訊,所以同時只能允許兩個賬號線上)
Socket網路程式設計(一)
此文使用的協議是 TCP 首先要寫入以下程式碼,不然很多函式都用不了 #include <WinSock2.h> #pragma comment(lib, "ws2_32.lib") ●伺服器端  
Unity+Lua互動篇
首先在我們偉大的GetHub上下載XLua框架。 今天就只發點基礎哈,畢竟第一次弄這個。。 本章主要說一些 Lua 的引用以及 C# 呼叫 Lua。 C#: //執行字串 //其中 Dostring 函式返回值即為程式碼塊裡 return 語句的返回值
unity Socket TCP連線案例(一)
非常清晰的demo 服務端 using System; using System.Collections; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using Syste
unity Socket TCP連接案例(一)
mon star connect color 服務器 void ipaddress field .get 非常清晰的demo 服務端 using System; using System.Collections; using System.Collectio
python運維-Socket網路程式設計
課程地址:https://www.imooc.com/learn/1031 一、 課程介紹 二、Socket通訊入門 1.服務端程式 相關引數有預設 同一時間只有1個被處理,可以掛起的
簡單的java socket TCP程式設計 每隔幾秒伺服器向客戶端傳時間
客戶端 package javaSocket; import java.io.*; import java.net.*; import org.junit.Test; import jinghai.base.time.LocalDateTime; import jinghai.base.uti
socket伺服器程式設計的多程序,多執行緒實現
socket伺服器程式設計: 顧名思義就是使用socket套接字來編寫伺服器程式的過程。不熟悉socket程式設計的小夥伴可以看我之前的文章,但是當時所實現的功能伺服器同時只能和一個客戶端進行互動,效率太低,利用多程序或者多執行緒方式來實現伺服器可以做到同時和多個客戶端進行互動。提高伺服器的效能