Home Page
Articoli
Tips & Tricks
News
Forum
Archivio Forum
Blogs
Sondaggi
Rss
Video
Utenti
Chi Siamo
Contattaci
Username:
Password:
Login
Registrati ora!
Recupera Password
Home Page
Stanze Forum
.NET Framework
Socket c# - ho preso spunto da JAVA
sabato 11 luglio 2009 - 14.51
Elenco Threads
Stanze Forum
Aggiungi ai Preferiti
Cerca nel forum
Argosk
Profilo
| Newbie
14
messaggi | Data Invio:
sab 11 lug 2009 - 14:51
Ciao ragazzi mi serve il vostro aiuto perchè non riesco a venirne a capo...
Ho creato una socket in c# che mi servirà per un progetto di chat in flash. Per creare la socket ho preso spunto da una server socket in java la quale diciamo che l'ho copiata. Provondo il socket in c# funziona con telnet senza problemi quando però richiamo la socket con flash mi fa inviare soltanto il primo messaggio a differenza che quello in java funziona perfettamente.. adesso inserisco i codici sia java che quello copiato in c# magari ho fatto qualche errore...
SOCKET IN JAVA
import java.io.*;
import java.net.*;
public class simpleServer
{
public static void main(String args[])
{
// Message terminator
char EOF = (char)0x00;
try
{
// create a serverSocket connection on port 9999
ServerSocket s = new ServerSocket(9999);
System.out.println("Server started. Waiting for connections...");
// wait for incoming connections
Socket incoming = s.accept();
BufferedReader data_in = new BufferedReader(
new InputStreamReader(incoming.getInputStream()));
PrintWriter data_out = new PrintWriter(incoming.getOutputStream());
data_out.println("Welcome! type EXIT to quit." + EOF);
data_out.flush();
boolean quit = false;
// Waits for the EXIT command
while (!quit)
{
String msg = data_in.readLine();
if (msg == null) quit = true;
if (!msg.trim().equals("EXIT"))
{
data_out.println("You sayed: <b>"+msg.trim()+"</b>"+EOF);
data_out.flush();
}
else
{
quit = true;
}
}
}
catch (Exception e)
{
System.out.println("Connection lost");
}
}
}
SOCKET IN C#
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Configuration;
class Server
{
public static void Main()
{
// Message terminator
char EOF = (char)0x00;
try
{
IPAddress ipAd = IPAddress.Parse("127.0.0.1");
/* Initializes the Listener */
TcpListener myList = new TcpListener(ipAd, 2055);
/* Start Listeneting at the specified port */
myList.Start();
Console.WriteLine("The server is running. Waiting for a connection....");
Socket soc = myList.AcceptSocket();
Stream s = new NetworkStream(soc);
StreamReader sr = new StreamReader(s);
StreamWriter sw = new StreamWriter(s);
sw.WriteLine("Benvenuti sul server! digita EXIT per disconnetterti." + EOF);
sw.Flush();
bool quit = false;
while (!quit)
{
string msg = sr.ReadLine();
if (msg == null) quit = true;
if (msg != "EXIT")
{
sw.WriteLine("Guest: <b>" + msg + "</b>" + EOF);
sw.Flush();
}
else
{
quit = true;
}
} // end while
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
Cosa ho sbagliato?
Torna su
Stanze Forum
Elenco Threads
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?
Dopo esserti registrato potrai chiedere
aiuto sul nostro
Forum
oppure aiutare gli altri
Consulta le
Stanze
disponibili.
Registrati ora !