Monday 18 March 2013

Posted by Prasad KM | 03:51 Categories:


C# Socket Client Program   


What Is a Socket?
A server application normally listens to a specific port waiting for connection requests from a client. When a connection request arrives, the client and the server establish a dedicated connection over which they can communicate. During the connection process, the client is assigned a local port number, and binds a socket to it. The client talks to the server by writing to the socket and gets information from the server by reading from it. Similarly, the server gets a new local port number (it needs a new port number so that it can continue to listen for connection requests on the original port). The server also binds a socket to its local port and communicates with the client by reading from and writing to it. The client and the server must agree on a protocol--that is, they must agree on the language of the information transferred back and forth through the socket.


Definition: A socket is one end-point of a two-way communication link between two programs running on the network.


The java.net package in the Java development environment provides a class--Socket--that represents one end of a two-way connection between your Java program and another program on the network. The Socket class implements the client side of the two-way link. If you are writing server software, you will also be interested in the ServerSocket class which implements the server side of the two-way link. This lesson shows you how to use the Socket and ServerSocket classes.
If you are trying to connect to the World Wide Web, the URL class and related classes (URLConnection, URLEncoder) are probably more suitable than the socket classes to what you are doing. In fact, URLs are a relatively high level connection to the Web and use sockets as part of the underlying implementation.
 The Program Socket Programming is used a starter for those who wants to write C# Codes using Net Sockets. It lets you to use Socket Connections with other Systems.
 The Server Waits for the Connection and gives a Warm Welcome Message to the User Or the Client.
Tools/API Information :
This Programme it Written using Net.Socket and no extra Feature is used. This is written with the Aim that anyone should understand the Usage of Sockets and NetWorks and to show that C# has got great Support for NetWorking.
Benefit :
There are lots of codes still I am sending this because of its Simplicity and you can use write this Code with just a NotePad/TextPad/Editor.
 

What is the difference between TCP and UDP?
TCP and UDP are both transport-level protocols. TCP is designed to provide reliable communication across a variety of reliable and unreliable networks and internets.

UDP provides a connectionless service for application-level procedures. Thus, UDP is basically an unreliable service; delivery and duplicate protection are not guaranteed.

What does a socket consists of?
The combination of an IP address and a port number is called a socket.

What are some advantages and disadvantages of Java Sockets?
Advantages of Java Sockets:
Sockets are flexible and sufficient. Efficient socket based programming can be easily implemented for general communications.
Sockets cause low network traffic. Unlike HTML forms and CGI scripts that generate and transfer whole web pages for each new request, Java applets can send only necessary updated information.

Disadvantages of Java Sockets:
Security restrictions are sometimes overbearing because a Java applet running in a Web browser is only able to establish connections to the machine where it came from, and to nowhere else on the network
Despite all of the useful and helpful Java features, Socket based communications allows only to send packets of raw data between applications. Both the client-side and server-side have to provide mechanisms to make the data useful in any way.
Since the data formats and protocols remain application specific, the re-use of socket based implementations is limited.

What is the difference between a NULL pointer and a void pointer?
A NULL pointer is a pointer of any type whose value is zero. A void pointer is a pointer to an object of an unknown type, and is guaranteed to have enough bits to hold a pointer to any object. A void pointer is not guaranteed to have enough bits to point to a function (though in general practice it does).


What is encapsulation technique?
ANSWER: Hiding data within the class and making it available only through the methods. This technique is used to protect your class against accidental changes to fields, which might leave the class in an inconsistent state.
How does the race condition occur?
It occurs when two or more processes are reading or writing some shared data and the final result depends on who runs precisely when.

What is multiprogramming?
Multiprogramming is a rapid switching of the CPU back and forth between processes.

Name the seven layers of the OSI Model and describe them briefly.
Physical Layer - covers the physical interface between devices and the rules by which bits are passed from one to another.
Data Link Layer - attempts o make the physical link reliable and provides the means to activate, maintain, and deactivate the link.
Network Layer - provides for the transfer of information between end systems across
some sort communications network.
Transport Layer - provides a mechanism for the exchange of data between end system.
Session Layer - provides the mechanism for controlling the dialogue between applications in end systems.
Presentation Layer - defines the format of the data to be exchanged between applications and offers application programs a set of data transformation services.
Application Layer - provides a means for application programs to access the OSI environment.



Client Code:
using System;using System.Net.Sockets;
public class Client
{
static public void Main( string[] Args )
{
TCPClient socketForServer;try{
socketForServer = new TCPClient("localHost", 10);
}catch{
Console.WriteLine(
"Failed to connect to server at {0}:999", "localhost");return;
}
NetworkStream networkStream = socketForServer.GetStream();
System.IO.StreamReader streamReader =new System.IO.StreamReader(networkStream);
System.IO.StreamWriter streamWriter =new System.IO.StreamWriter(networkStream);try{string outputString;// read the data from the host and display it{
outputString = streamReader.ReadLine();
Console.WriteLine(outputString);
streamWriter.WriteLine("Client Message");
Console.WriteLine("Client Message");
streamWriter.Flush();
}
}
catch
{

Console.WriteLine("Exception reading from Server");

}
// tidy upnetworkStream.Close();

}
}
 

0 comments:

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube