Skip to content

Commit

Permalink
Modified the samples and published ssl package to nuget
Browse files Browse the repository at this point in the history
  • Loading branch information
mikoskinen committed Jul 26, 2012
1 parent 28360c4 commit 7bacc34
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 50 deletions.
Binary file added nuget/SocketEx.2.0.0.nupkg
Binary file not shown.
Binary file added nuget/SocketEx.SSL.1.0.0.nupkg
Binary file not shown.
27 changes: 25 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
# SocketEx.TcpClient - TcpClient for Windows Phone #
# SocketEx - TcpClient and SSL Socket support for Windows Phone #

SocketEx.TcpClient is a MIT-licensed TcpClient for Windows Phone which aims to make working with Windows Phone sockets easy. Compared to the TcpClient in full .NET Framework, SocketEx.TcpClient isn’t 100% compatible and some of the features aren’t implemented at all. The library hasn’t gone through an exhaustive testing so there may be issues.

SocketEx.SecureTcpClient is a TcpClient which provides SSL Socket support to Windows Phone.

Note! The library works in a synchronous blocking mode. This means that if you use the TcpClient directly from the UI-thread, you will block the UI from updating.

The code is based on the “Crystalbyte Networking for Silverlight” project, available from the CodePlex. Almost all of the code is from that neat library, but I adjusted it little to get it working with Windows Phone and fixed out some threading issues.
The code for TcpClient is based on the “Crystalbyte Networking for Silverlight” project, available from the CodePlex. Almost all of the code is from that neat library, but I adjusted it little to get it working with Windows Phone and fixed out some threading issues.

SecureTcpClient uses Bouncy Castle to provide the SSL support.

## Nuget ##
Both the TcpClient and SecureTcpClient are available from NuGet:

* Install-Package SocketEx
* Install-Package SocketEx.SSL


## SocketEx.TcpClient – How To Open a Connection ##
We can open the connection by passing the server address and server port as parameters to TcpClient.
Expand Down Expand Up @@ -42,6 +53,18 @@ To write a message we need a StreamWriter.
writer.WriteLine(request);
}

##SocketEx.SecureTcpClient - How To open a Secure Connection ##

private SecureTcpClient CreateConnection()
{
var connection = new SecureTcpClient(serverAddress, serverPort);

return connection;
}

## 3rd part libraries ##
SocketEx.SecureTcpClient uses modified version of Bouncy Castle to create the SSL connections.
## Contact Author ##

Mikael Koskinen - [http://mikaelkoskinen.net]()
7 changes: 4 additions & 3 deletions src/securetcpclient_sample/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@

<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="TCP CLIENT" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="ApplicationTitle" Text="SECURE TCP CLIENT" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="sample" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>

<!--ContentPanel - place additional content here-->
<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Content="Use SecureTcpClient" Click="UseSecureClientClick"/>
<TextBox x:Name="Content" AcceptsReturn="True" Height="500" TextWrapping="Wrap" IsReadOnly="True"/>
<Button Content="Open SSL Socket" Click="UseSecureClientClick"/>
<TextBlock Style="{StaticResource PhoneTextTitle2Style}" x:Name="Address"/>
<TextBox Grid.Row="1" Grid.Column="1" x:Name="ContentSSL" AcceptsReturn="True" Height="500" TextWrapping="Wrap" IsReadOnly="True"/>
</StackPanel>
</Grid>

Expand Down
30 changes: 13 additions & 17 deletions src/securetcpclient_sample/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.IO;
using System.Text;
using System.Threading;
using System.Windows;
using Org.BouncyCastle.Crypto.Tls;
using SocketEx;

namespace securetcpclient_sample
Expand All @@ -14,6 +14,7 @@ public partial class MainPage
public MainPage()
{
InitializeComponent();
this.Address.Text = string.Format("{0}:{1}", serverAddress, serverPort);
}

private void UseSecureClientClick(object sender, RoutedEventArgs e)
Expand All @@ -22,29 +23,24 @@ private void UseSecureClientClick(object sender, RoutedEventArgs e)
var stream = connection.GetStream();

var reader = new StreamReader(stream);
var writer = new StreamWriter(stream);
var request = "GET / HTTP/1.1\r\nHost: " + serverAddress + "\r\nConnection: Close\r\n\r\n";

ThreadPool.QueueUserWorkItem(x =>
{
var fullMessage = new StringBuilder();
string message;
while ((message = reader.ReadLine()) != null)
{
fullMessage.AppendLine(message);
}
Dispatcher.BeginInvoke(() => Content.Text = fullMessage.ToString());
writer.WriteLine(request);
writer.Flush();

});
var fullMessage = new StringBuilder();

using (var writer = new StreamWriter(stream))
string message;
while ((message = reader.ReadLine()) != null)
{
var request = "GET / HTTP/1.1\r\nHost: " + serverAddress + "\r\nConnection: Close\r\n\r\n";
if (string.IsNullOrWhiteSpace(message))
break;

writer.WriteLine(request);
fullMessage.AppendLine(message);
}

Thread.Sleep(1500);
ContentSSL.Text = fullMessage.ToString();
}

private SecureTcpClient CreateConnection()
Expand Down
12 changes: 6 additions & 6 deletions src/sslsocket/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("sslsocket")]
[assembly: AssemblyTitle("SSL Socket for Windows Phone")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("sslsocket")]
[assembly: AssemblyCopyright("Copyright © 2012")]
[assembly: AssemblyCompany("Mikael Koskinen")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("Copyright © Mikael Koskinen 2012")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand All @@ -32,6 +32,6 @@
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("2.0.0")]
[assembly: AssemblyFileVersion("2.0.0")]
[assembly: NeutralResourcesLanguageAttribute("en-US")]
4 changes: 2 additions & 2 deletions src/tcpclient/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("2.0.0")]
[assembly: AssemblyFileVersion("2.0.0")]
[assembly: NeutralResourcesLanguage("en-US")]
32 changes: 12 additions & 20 deletions src/tcpclient_sample/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Diagnostics;
using System.IO;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows;
using SocketEx;

Expand All @@ -24,29 +21,24 @@ private void UseClientClick(object sender, RoutedEventArgs e)
var stream = connection.GetStream();

var reader = new StreamReader(stream);
var writer = new StreamWriter(stream);
var request = "GET / HTTP/1.1\r\nHost: " + serverAddress + "\r\nConnection: Close\r\n\r\n";

ThreadPool.QueueUserWorkItem(x =>
{
var fullMessage = new StringBuilder();
writer.WriteLine(request);
writer.Flush();

string message;
while ((message = reader.ReadLine()) != null)
{
fullMessage.AppendLine(message);
}
Dispatcher.BeginInvoke(() => Content.Text = fullMessage.ToString());
var fullMessage = new StringBuilder();

});

using (var writer = new StreamWriter(stream))
string message;
while ((message = reader.ReadLine()) != null)
{
var request = "GET / HTTP/1.1\r\nHost: " + serverAddress + "\r\nConnection: Close\r\n\r\n";
if (string.IsNullOrWhiteSpace(message))
break;

writer.WriteLine(request);
fullMessage.AppendLine(message);
}

Thread.Sleep(1500);
Content.Text = fullMessage.ToString();
}

private TcpClient CreateConnection()
Expand Down

0 comments on commit 7bacc34

Please sign in to comment.