1 Replies - 500 Views - Last Post: 26 December 2011 - 04:48 AM Rate Topic: -----

#1 Shazer2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-December 11

Small socket work

Posted 25 December 2011 - 03:51 PM

I am trying to set up a client/server relationship, so I can eventually send a message to the server, and then each client will receive the message sent. I currently have the following code:

Server.cs
/*
 * Created by SharpDevelop.
 * User: Shannon
 * Date: 12/26/2011
 * Time: 9:32 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Net;
using System.Net.Sockets;

namespace Server
{
	class Program
	{
		public static void Main(string[] args)
		{
			IPAddress ip = IPAddress.Parse("127.0.0.1");
			int port = 2222;
			byte[] buffer = new byte[256];
			
			IPEndPoint IPe = new IPEndPoint(ip, port);
			
			Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
			sock.Bind(IPe);
			sock.Listen(0);
			sock.Accept();
			sock.Receive(buffer, 256, SocketFlags.None);
			sock.Send(buffer, 256, SocketFlags.None);
			sock.Close();
		}
	}
}



Client.cs
/*
 * Created by SharpDevelop.
 * User: Shannon
 * Date: 12/26/2011
 * Time: 9:01 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Net.Sockets;
using System.Net;

namespace Client
{
	class Program
	{
		public static void Main(string[] args)
		{	
			IPAddress ip = IPAddress.Parse("127.0.0.1");
			int port = 2222;
			byte[] buffer = new byte[256];
			
			EndPoint ep = new IPEndPoint(ip, port);
			
			Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
			sock.Connect(ep);
			
			byte[] msg = Console.ReadLine(); // Can't convert from byte[] to string, not sure what to do.
			
			sock.Send(msg, 256, SocketFlags.None);
			sock.Receive(buffer, 256, SocketFlags.None);
			
			
			sock.Close();
		}
	}
}



I'm just having trouble with the input. I've tried to search up client/server examples, but nobody seems to be doing anything similar.

Is This A Good Question/Topic? 0
  • +

Replies To: Small socket work

#2 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 178
  • View blog
  • Posts: 750
  • Joined: 27-October 11

Re: Small socket work

Posted 26 December 2011 - 04:48 AM

Take a look at a tutorial here:

Basic Client/Server Chat Application in C#

And I'm not sure how you concluded that there are no examples of what you're looking for.

There's a TON of information on Google

Introduction to TCP client server in C#
Simple Client-server Interactions using C#
C# Tutorial - Simple Threaded TCP Server
Asynchronous Socket Programming in C#: Part I
C# Client Socket program
A Chat Client/Server Program for C#

And remember we're not your google-bots, before asking questions please do a proper search yourself.

Take a look at these tutorials on how to do a proper search:

The Basic Guide to MSDN
Getting what you need by doing a decent search of the internet

This post has been edited by RexGrammer: 26 December 2011 - 05:03 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1