TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Carloine menths
NA
41
32.2k
Server and client connection
Feb 11 2018 6:27 PM
Hello,
I want to send data to server the problem is only the first data is sent .
server code:
namespace
Server
{
class
Program
{
static
byte
[] Buffer {
get
;
set
; }
static
Socket sck;
static
void
Main(
string
[] args)
{
sck =
new
Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sck.Bind(
new
IPEndPoint(0, 2000));
sck.Listen(100);
Socket accepted = sck.Accept();
Buffer =
new
byte
[accepted.SendBufferSize];
int
bytesRead = accepted.Receive(Buffer);
byte
[] formatted =
new
byte
[bytesRead];
for
(
int
i = 0; i < bytesRead; i++)
{
formatted[i] = Buffer[i];
}
string
strData = Encoding.ASCII.GetString(formatted);
Console.Write(strData +
"\r\n"
);
Console.Read();
sck.Close();
accepted.Close();
}
}
}
client code:
namespace
Client
{
class
Program
{
static
Socket sck;
static
void
Main(
string
[] args)
{
sck =
new
Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint localEndPoint =
new
IPEndPoint(IPAddress.Parse(
"127.0.0.1"
), 2000);
try
{
sck.Connect(localEndPoint);
}
catch
{
Console.Write(
"Unable to connect to remote end point!\r\n"
);
Main(args);
}
Console.Write(
"Your name: "
);
string
text = Console.ReadLine();
byte
[] data = Encoding.ASCII.GetBytes(text);
sck.Send(data);
Console.Read();
Console.Write(
"Your age: "
);
string
text1 = Console.ReadLine();
byte
[] data1 = Encoding.ASCII.GetBytes(text1);
sck.Send(data1);
Console.Write(
"Data Sent!\r\n"
);
Console.Write(
"Press any key to continue..."
);
Console.Read();
sck.Close();
}
}
}
Reply
Answers (
1
)
Coding to analyse and validate data
How do I take to inputs in c# and out put them as outputs