I am new to C#. I am trying to establish a TCP communication using TLS over LAN but have no idea how to do it. I have done the same in java using following code :
SSLSocketFactory sslSocketFactory = Util.BuildSslSocketFactoryForLAN(getApplicationContext());
Socket tcpSocket = sslSocketFactory.createSocket("IP address", "port");
is = tcpSocket.getInputStream();
OutputStream os = tcpSocket.getOutputStream();
devicePairingReq = Util.hexStringToByteArray("9F" + data);
os.write(devicePairingReq);
os.flush();
while (true) {
tcpSocket.setSoTimeout(1000 * 90);
resp = new byte[15];
test = is.read(resp);
tcpSocket.close();
public static SSLSocketFactory BuildSslSocketFactoryForLAN(Context context) {
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream is = context.getResources().getAssets().open("name.pem");
InputStream caInput = new BufferedInputStream(is);
Certificate ca;
try {
ca = cf.generateCertificate(caInput);
} finally {
caInput.close();
}
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);
sslContext.init(null, tmf.getTrustManagers(), null);
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
return sslSocketFactory;
} catch (CertificateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
return null;
}
I am totally new to C# so have very little idea about it. I am trying to sent a message to tcp server from mobile client over LAN.