Send data over the network C#
By : user2537965
Date : March 29 2020, 07:55 AM
may help you . If you are using a connectionless protocol, you must call Connect before calling Send, or Send will throw a SocketException. If you are using a connection-oriented protocol, you must either use Connect to establish a remote host connection, or use Accept to accept an incoming connection. Refer Socket.Send Method (Byte[], Int32, SocketFlags)Assuming you are using a connectionless protocol the code should be like this, code :
string response = "Hello";
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
if (ipAddress != null)
{
IPEndPoint serverEndPoint = new IPEndPoint(ipAddress, 25);
byte[] receiveBuffer = new byte[100];
try
{
using (TcpClient client = new TcpClient(serverEndPoint))
{
using (Socket socket = client.Client)
{
socket.Connect(serverEndPoint);
byte[] data = Encoding.ASCII.GetBytes(response);
socket.Send(data, data.Length, SocketFlags.None);
socket.Receive(receiveBuffer);
Console.WriteLine(Encoding.ASCII.GetString(receiveBuffer));
}
}
}
catch (SocketException socketException)
{
Console.WriteLine("Socket Exception : ", socketException.Message);
throw;
}
}
|
Should I use UTF-8 to send data over the network?
By : user3354753
Date : March 29 2020, 07:55 AM
wish helps you With UTF-8 encoding, you'll use: 1 byte for ASCII chars 2 bytes for unicode chars between U+0000 and U+07FF more bytes if necesseray
|
When no network - save data to sqlite and send to server once network regained
By : Ankit Kothari
Date : March 29 2020, 07:55 AM
Hope this helps You can save data to SQLite even if there is a network connection and then send it to API. In that case you can use only statemen in else from first step. Below is the scenario you are looking for. code :
callApi(){
if(this._connectivityService.isOnline()){
this.service.sendAction(itemID).subscribe(result => {})
} else {
this._databaseService.sendAction(itemID).then(result => {})
}
}
ionViewDidEnter(){
this.sendData();
}
sendData(){
this.count = 0;
this._database.getLocalData().then((result) => {
this.DataList = <Array<Object>> result;
if(this.DataList.length !== 0){
this.DataList.forEach(function(item) {
this.service.sendAction(item).subscribe(result => {
if(res.status == "ok" && this.count == this.DataList.length-1){
//empty SQLite local table if success
this._database.deleteLocalData();
}
})
}
} else {
//
}
}, (error) => {
console.log("Offline data not sent!",
error);
});
}
public getLocalData() {
return new Promise((resolve, reject) => {
this.sqlite.create({
name: 'dbName.db',
location: 'default'
})
.then((db: SQLiteObject) => {
db.executeSql("SELECT * FROM tableName", []).then((data) => {
let DataList = [];
if(data.rows.length > 0) {
for(let i = 0; i < data.rows.length; i++) {
DataList.push({
itemID: data.rows.item(i).itemID,
something: data.rows.item(i).something
});
}
}
resolve(DataList);
}, (error) => {
reject(error);
});
})
.catch(e => console.log(e));
});
}
|
detect if device connect to which network and then send data from server depends on which network connected
By : user1536602
Date : March 29 2020, 07:55 AM
Hope this helps I want to check if my android device connected to which network (either wifi, home network, or mobile network) and then I want to use different the url link (use to send data to server) depends on which network the device connected. I'm new to android, and I already googled it but doesn't seems to find the answer or because of my limited vocabulary. Can anybody help me? , try to use this mate code :
final ConnectivityManager connMgr = (ConnectivityManager)
getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isConnected()) {
//do what you want
} else if (mobile.isConnected()) {
//do what you want
}
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
|
Is data secure as long as you send it from/to an HTTPS page? I can still see form data in chrome's network devoloper too
By : Richard Guan
Date : March 29 2020, 07:55 AM
wish helps you https secures only the connection from the user to the server. Once they are at the server they are no longer protected by https against attacks, so you have to find others ways to protect them there. https will also not protect the data from against attacks against the logic of the web application, like CSRF, XSS, clickjacking etc. In short: https is just one part to secure the data, there is much more.
|